Translate () takes exactly one argument (2 given) in python error
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()
An error is displayed here due to line feed ... help me what to do next .. I am using translation to remove a digit from the 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)
source to share
str.translate
requires dict
one that maps unicode ordinal numbers to other unicode ordinals (or None
if you want to remove a character). You can create it like this:
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, the easiest way to make a table is using a function str.maketrans
. It can take various arguments, but you need the third form, arg. We ignore the first two arguments as they are meant to map characters to other characters. The third arg is the characters you want 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"
source to share
Higher versions in Python use this:
eg: oldname= "delhi123"
remove="1234567890"
table=str.maketrans("","",remove)
oldname.translate(table)
General solution for your request:
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()
source to share
This is how translation works:
yourstring.translate(str.maketrans(fromstr, tostr, deletestr));
Replace characters in fromstr with the character at the same position in tostr and remove all characters that are in deletestr. Fromstr and tostr can be empty strings, and the deletestr parameter can be omitted.
source to share
This function
str.translate(table[, deletechars])
only available for python 2.7. If you are using a higher version, you can use the following function, which is quiet and available in a higher version of python.
bytes.translate(table[, delete=b''])
Returns a copy of the bytes object, which removes any bytes that occur when the optional argument is removed
So, in your code, change this line of code
os.rename(file_name,file_name.translate(None,"0123456789"))
from
file_name_bytes = str.encode(file_name)
os.rename(file_name, file_name_bytes.translate(None, b"0123456789")
source to share
If all you want to do is do the same thing that you did in Python 2 in Python 3, here's what I did in Python 2.0 to get rid of the punctuation marks and numbers:
text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')
Here's my Python 3.0 equivalent:
text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))
Basically, it says "translate nothing" (the first two parameters) and translate any punctuation marks or numbers to "None" (ie remove them).
source to share
Run the file using the python secret_message.py command. It works.
here is all the code:
import os
def rename_files (): # (1) get filenames from 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: "+ current_dir) os.chdir (r" / Users / archananagaraja / Desktop / AN_Personal / Udacity_Python / prank ") # (2) for each file, rename the file name for filename to file_list: os.rename (filename), file_name.translate (None, "0123456789")) rename_files ()
source to share