2
import os
import re

def rename_files():
    # get the files from dir
    file_list=os.listdir(r"C:\OOP\prank")
    print(file_list)
    saved_path=os.getcwd()
    print("current working directory"+saved_path)
    os.chdir(r"C:\OOP\prank")
    #rename the files
    for file_name in file_list:
        print("old name-"+file_name)
        #print("new name-"+file_name.strip("0123456789"))
        os.rename(file_name,file_name.translate(None,"0123456789"))
        os.chdir(saved_path)

rename_files()

Here error is showing due to translate line ...help me what to do next ..I am using translate to remove the digit from filename.

Traceback (most recent call last):
    File "C:\Users\vikash\AppData\Local\Programs\Python\Python35-  32\pythonprogram\secretName.py", line 17, in <module>
rename_files()
      File "C:\Users\vikash\AppData\Local\Programs\Python\Python35-  32\pythonprogram\secretName.py", line 15, in rename_files
     os.rename(file_name,file_name.translate(None,"0123456789"))
     TypeError: translate() takes exactly one argument (2 given)
Mureinik
  • 252,575
  • 45
  • 248
  • 283
vikash
  • 41
  • 1
  • 1
  • 2
  • 2
    What is the desired outcome? Your indentation is wrong too. – MooingRawr Sep 07 '16 at 17:11
  • ya it looks like ..but the desired outcome is to remove the digits from all the filenames in my given path.For e.g ;23log.jpg become log.jpg – vikash Sep 07 '16 at 17:15
  • 2
    Are you updating code to work on python3.x? If so, this is probably a dupe of http://stackoverflow.com/questions/1324067/how-do-i-get-str-translate-to-work-with-unicode-strings – mgilson Sep 07 '16 at 17:17
  • Look at this link for an example of how to make to work. http://www.tutorialspoint.com/python/string_translate.htm – Michael Platt Sep 07 '16 at 17:18

11 Answers11

10

str.translate requires a dict that maps unicode ordinals to other unicode oridinals (or None if you want to remove the character). You can create it like so:

old_string = "file52.txt"
to_remove = "0123456789"
table = {ord(char): None for char in to_remove}
new_string = old_string.translate(table)
assert new_string == "file.txt"

However, there is simpler way of making a table though, by using the str.maketrans function. It can take a variety of arguments, but you want the three arg form. We ignore the first two args as they are for mapping characters to other characters. The third arg is characters you wish to remove.

old_string = "file52.txt"
to_remove = "0123456789"
table = str.maketrans("", "", to_remove)
new_string = old_string.translate(table)
assert new_string == "file.txt"
Dunes
  • 32,114
  • 7
  • 68
  • 83
6

Higher versions in Python use this :

eg: oldname= "delhi123"    
remove="1234567890"    
table=str.maketrans("","",remove)    
oldname.translate(table)    

Overall solution for your query:

import os    

def rename_file_names():    
    file_list=os.listdir(r"C:\Users\welcome\Downloads\Compressed\prank")    
    print (file_list)    
    saved_path=os.getcwd()    
    print("current working direcorty is"+saved_path)    
    os.chdir(r"C:\Users\welcome\Downloads\Compressed\prank")    
    remove="123456789"    
    table=str.maketrans("","",remove)    
    for file_name in file_list:    
        os.rename(file_name,file_name.translate(table))    


rename_file_names()    
Martin Evans
  • 37,882
  • 15
  • 62
  • 83
Venkat
  • 79
  • 2
  • 6
2

Change os.rename(file_name,file_name.translate(None,"0123456789")) to os.rename(file_name,file_name.translate(str.maketrans('','',"0123456789"))) and it will work.

SUMIT RAJ
  • 21
  • 3
1

If all you are looking to accomplish is to do the same thing you were doing in Python 2 in Python 3, here is what I was doing in Python 2.0 to throw away punctuation and numbers:

text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')

Here is my Python 3.0 equivalent:

text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))

Basically it says 'translate nothing to nothing' (first two parameters) and translate any punctuation or numbers to None (i.e. remove them).

adiga
  • 28,937
  • 7
  • 45
  • 66
1
import os

def rename_files():
    #1 get file names from folder
    list_files = os.listdir(r"C:\Personal\Python\prank")
    print(list_files)
    saved_path = os.getcwd()
    print(os.getcwd())
    os.chdir(r"C:\Personal\Python\prank\")

    #2 for each file, rename filename
    remove = "0123456789"
    table = str.maketrans("","",remove)
    for file_name in list_files:
        os.rename(file_name, file_name.translate(table))

    os.chdir(saved_path)

rename_files()
Kinchit
  • 23
  • 4
0

Instead of translate why not just do this:

os.rename(file_name,''.join([i for i in file_name if not i.isdigit()]))
MooingRawr
  • 4,446
  • 3
  • 24
  • 29
0

if you use the python 3.X,try this:file_name.lstrip()

    os.rename(file_name,file_name.lstrip(None,"0123456789"))
FiFi.X
  • 1
0

This function

str.translate(table[, deletechars])

is only available upto python 2.7. If you are using higher version you can use following function which is quiet similar and available in higher version of python.

bytes.translate(table[, delete=b''])

It return a copy of the bytes object where all bytes occurring in the optional argument delete are removed

So In your code, change this line of code

os.rename(file_name,file_name.translate(None,"0123456789"))

with

file_name_bytes = str.encode(file_name)
os.rename(file_name, file_name_bytes.translate(None, b"0123456789") 
Sanjit Singh
  • 198
  • 2
  • 9
0

Run the file with the command python secret_message.py . It works.

here is the entire code:

import os

def rename_files(): #(1)get file names from a folder file_list = os.listdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") print(file_list) current_dir = os.getcwd() print("My current directory is: "+current_dir) os.chdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") #(2) for each file rename filename for file_name in file_list: os.rename(file_name, file_name.translate(None, "0123456789")) rename_files()

Archana
  • 499
  • 1
  • 3
  • 10
0

instead of using function getcwd() it just works

import os 

def rename_files():
    #(1) get file names from a folder 

    file_list = os.listdir(r"D:\prank\prank")
    os.chdir(r"D:\prank\prank")
    #remove any numbers
    remove = "0123456789"
    chart = str.maketrans("","", remove)
    # files name renamed  
    for file_name in file_list:
        os.rename(file_name, file_name.translate(chart))
        print(file_name)
Markus
  • 1,485
  • 4
  • 18
  • 33
Almadani
  • 83
  • 1
  • 3
0

Try this:

"".join(c for c in "123hello.jpg" if c not in "0123456789")

list comprehensions: https://medium.com/swlh/list-comprehensions-in-python-3-for-beginners-8c2b18966d93

princebillyGK
  • 987
  • 11
  • 13