Translate function in Python 3

Dean Clancy picture Dean Clancy · Jan 18, 2017 · Viewed 51k times · Source

I am using Python 3 and I want to translate my file names to have no numbers. The translate function doesn't seem to work in Python 3. How can I translate the file names to have no numbers?

This is the block of code that doesn't work:

file_name = "123hello.jpg"
file_name.translate(None, "0123456789")

Thanks

Answer

wim picture wim · Jan 18, 2017

str.translate is still there, the interface has just changed a little:

>>> table = str.maketrans(dict.fromkeys('0123456789'))
>>> '123hello.jpg'.translate(table)
'hello.jpg'