Python Forum
Help deleting numbers from file names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help deleting numbers from file names
#1
import os
def rename_files():
# Get file names
    file_list= os.listdir(r"file")
    #print(file_list)
    saved_path=os.getcwd()
    print("Current Working Directory Is " +saved_path)
    os.chdir(r"file")
# Rename files
    for file_name in file_list:
          os.rename(file_name, file_name.translate(None, '0123456789'))
    os.chdir(saved_path)

rename_files()
Reply
#2
It will be very helpful if you can ask specifically what trouble you are having with the code. You can also include the full error message in error tags.
Reply
#3
The goal is deleting numbers from file names.
Error:
Traceback (most recent call last): File "C:\Users\laris\AppData\Local\Programs\Python\Python36-32\photofile.py", line 14, in <module> rename_files() File "C:\Users\laris\AppData\Local\Programs\Python\Python36-32\photofile.py", line 11, in rename_files os.rename(file_name, file_name.translate(None, '0123456789')) TypeError: translate() takes exactly one argument (2 given)
Reply
#4
Use re.sub(r'[0-9]', '', file_name) instead of file_name.translate()
Reply
#5
Translate as changed in Python 3,so now it only take one argument(translation table).
Can use it like this or regex as Grib posted.
>>> s = 'fo34o454343bar5345.py'
>>> s.translate({ord(c):'' for c in "1234567890"})
'foobar.py'
The trix is also to set path,then change to that path.
Then dos not need to use os.path.join() to get absolute path,
which is needed if script run a other folder than files to be renamed.
import os

path = r'C:\1\sp'
file_list = os.listdir(path)
os.chdir(path)
for file_name in file_list:
    print(file_name)
    os.rename(file_name, file_name.translate({ord(c):'' for c in "1234567890"}))
Output:
# Before Makefile modu3424le.py Pip4345435ile Pip535345fle.lock # After Makefile module.py Pipfle.lock Pipile
Reply
#6
In[1]: table = {ord(str(num)): None for num in range(10)}

In[2]: table
Out[2]: 
{48: None,
 49: None,
 50: None,
 51: None,
 52: None,
 53: None,
 54: None,
 55: None,
 56: None,
 57: None}

In[3]: "001file_name.txt".translate(table)
Out[3]: 'file_name.txt'
To create this table you can use str.maketrans method too.
table = str.maketrans('', '', '0123456789') # the characters from the third argument will be mapped to None
You get the same table:
{48: None,
 49: None,
 50: None,
 51: None,
 52: None,
 53: None,
 54: None,
 55: None,
 56: None,
 57: None}
print(str.maketrans.__doc__)
Output:
Return a translation table usable for str.translate(). If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None. Character keys will be then converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  deleting columns in CSV file astral_travel 8 2,326 Nov-26-2022, 09:36 PM
Last Post: astral_travel
  rename same file names in different directories elnk 0 707 Nov-04-2022, 05:23 PM
Last Post: elnk
  Calcolate the average of numbers from a .txt file francescomiles 2 3,010 Mar-27-2021, 02:43 PM
Last Post: francescomiles
  Read strings and numbers in columns from a file suvadip 4 2,868 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Saving Excel workbook file with dataframe names Biplab1985 0 2,022 Jun-07-2020, 12:25 PM
Last Post: Biplab1985
  Deleting a line in a csv file julio2000 6 3,077 Feb-25-2020, 05:12 PM
Last Post: julio2000
  Removing Certain Numbers From File chascp 2 2,076 Feb-07-2020, 04:04 PM
Last Post: chascp
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,688 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  splitstring file names a by hyphen steve22020 3 3,268 Oct-30-2019, 05:39 PM
Last Post: steve22020
  Is there a more effecient way to do this ? File Names sumncguy 2 2,076 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020