I am currently participating in an Udacity course that instructs students on programming using Python. One of the projects has students rename photo files (remove any numbers in the name) in a directory in order to have the files arranged alphabetically, after which a secret message will be spelled out. For instance, if a file name is "48athens"
, the program seeks to remove the numbers, leaving only "athens"
as the file name.
I am using Python 3.6, while the course instructor is using Python 2.7. I should likely be using Python 2.7 so as to simplify the learning process. However, for now I will keep using Python 3.6.
The way in which the instructor has the files renamed is using the .translate
function, which takes two arguments in Python 2.x, while Python 3.x only takes one argument. It removes any numbers (0 through 9) from the file names.
import os
def rename_files(): #Obtain the file names from a folder.
file_list = os.listdir(r"C:UsersDennisDesktopOOPprankprank")
print (file_list)
saved_path = os.getcwd()
os.chdir(r"C:UsersDennisDesktopOOPprankprank")
for file_name in file_list: #Rename the files inside of the folder.
os.rename(file_name, file_name.translate(None, "0123456789"))
os.chdir(saved_path)
rename_files()
However, this does not work in Python 3.x, as it says that:
TypeError: translate() takes exactly one argument (2 given)
Thankfully, I found another way using someone's assistance. However, I'm not really sure how it works. Can someone explain the str.maketrans
function to me, and what the first two blank arguments in quotes are for? My thought is that it's saying: for the first two characters in the file name, remove any numbers (0 through 9). Is that correct? For instance, in "48athens"
, remove the first two characters (4 and 8) if they are numbers between 0 and 9.
import os
def rename_files(): #Obtain the file names from a folder.
file_list = os.listdir(r"C:UsersDennisDesktopOOPprankprank")
print (file_list)
saved_path = os.getcwd()
os.chdir(r"C:UsersDennisDesktopOOPprankprank")
for file_name in file_list: #Rename the files inside of the folder.
os.rename(file_name, file_name.translate(str.maketrans('','','0123456789')))
os.chdir(saved_path)
rename_files()
My Understanding of the Documentation:
static str.maketrans(x[, y[, z]])
This static method returns a translation table usable for str.translate()
.
It's saying that the arguments passed to str.maketrans
, along with the actual function str.maketrans
, will make a table that says, "If this character appears, replace it with this character." However, I'm not sure what the brackets are for.
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters (strings of length 1) to Unicode
ordinals, strings (of arbitrary lengths) or None. Character keys will
then be converted to ordinals.
It's saying that it can only change integers, or characters in strings of length one, to other integers or strings (of any length you want). But I believe I have three arguments, not one.
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.
I have three arguments ('', '', '0123456789')
. I think x
is the first ''
, and y
is the second ''
. I have the third argument, which is a string '0123456789'
, but I don't understand what it means to be mapped to 'None'
.
See Question&Answers more detail:
os