Trying to learn Python

4ftersh0ck

Expert Member
Joined
Feb 22, 2011
Messages
1,019
Hi guys,

Please can someone assist me. I have just started a Nanodegree at Udacity (Full Stack Web Developer) and I am stuck on something. I have followed the code (character, by character) from the video and I still get an error. Where did I go wrong? Thank you for any advice

Code:-

import os
def rename_files() :
#(1) get file names from a folder
file_list = os.listdir(r"C:\Udacity\prank")
#print(file_list)
saved_path = os.getcwd()
print("Current Working Directory is "+saved_path)
os.chdir(r"C:\Udacity\prank")
#(2) for each file, rename filename
for file_name in file_list:
os.rename(file_name, file_name.translate(None, "0123456789"))
os.chdir(saved_path)

rename_files()

Error message:-

Traceback (most recent call last):
File "C:\Users\Paul\AppData\Local\Programs\Python\Python35\rename_files20161026.py", line 14, in <module>
rename_files()
File "C:\Users\Paul\AppData\Local\Programs\Python\Python35\rename_files20161026.py", line 11, in rename_files
os.rename(file_name, file_name.translate(None, "0123456789"))
TypeError: translate() takes exactly one argument (2 given)
 

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
42,920
Well, the error is pretty clear from that stacktrace: translate() takes exactly one argument.

Looking at your code you gave it two: None and "0123456789"
 

Johnatan56

Honorary Master
Joined
Aug 23, 2013
Messages
30,955
Well, the error is pretty clear from that stacktrace: translate() takes exactly one argument.

Looking at your code you gave it two: None and "0123456789"

Probably
os.rename(file_name, file_name.translate("0123456789"))
then
 

4ftersh0ck

Expert Member
Joined
Feb 22, 2011
Messages
1,019
Thanks for the response guys. I am running Python 3. The 'None' in my code was there because I am not using a table. In the tutorial they said that the first entry in the brackets was your source table and if you weren't using one you put in 'None' followed by comma.
 

4ftersh0ck

Expert Member
Joined
Feb 22, 2011
Messages
1,019
Thanks Today for the advice. Maybe I should do that, otherwise every time there is an error in my code I will be left wondering if it is me that made the mistake or the version.
 

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
42,920
Thanks Today for the advice. Maybe I should do that, otherwise every time there is an error in my code I will be left wondering if it is me that made the mistake or the version.

That or you could look at the last couple of lines of the error and google it. You're never the first guy to experience a specific error and there's almost always an answer available on Stackoverflow or similar.
 
Top