I am trying to translate file name without number. I am getting following error:
>>> file ="1abc89.jpg"
>>> os.rename(file,file.translate(None,"0123456789"))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: expected a character buffer object
I tried searching few threads, but am not able to get the point.
Python 3.4:
>>> import os
>>> name = '1abc89.jpg'
>>> name.translate(str.maketrans('','','0123456789'))
'abc.jpg'
>>> os.rename(name,name.translate(str.maketrans('','','0123456789')))
Python 2.7:
>>> import os
>>> name = '1abc89.jpg'
>>> name.translate(None,'0123456789')
'abc.jpg'
>>> os.rename(name,name.translate(None,'0123456789'))
References:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.