views:

899

answers:

2

My problem is similar to http://stackoverflow.com/questions/497233/pythons-os-path-choking-on-hebrew-filenames

however, I don't know the original encoding of the filename I need to rename (unlike the other post he knew it was Hebrew originally).

I was doing data recovery for a client and copied over the files to my XP SP3 machine, and some of the file names have "?" replacing/representing invalid characters.

I tried to use Python to os.rename the files since I know it has unicode support, however, when I tell python to rename the files, it seems it's unable to pass a valid file name back to the windows API.

i.e.:

>>> os.chdir(r'F:\recovery\My Music')
>>> os.listdir(u'.')
[u'Don?t Be Them.mp3', u'That?s A Soldier.mp3']
>>> blah=os.listdir(u'.')
>>> blah[0]
Don?t Be Them.mp3
>>> os.rename(blah[0],'dont be them.mp3')

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    os.rename(blah[0],'dont be them.mp3')
WindowsError: [Error 123] The filename, directory name, or
  volume label syntax is incorrect

I'm using Python 2.6, on Win XP SP3, with whatever encoding is standard XP behavior for US/English.

Is there a way to handle these renames without knowing the original language?

A: 

Try passing a unicode string:

os.rename(blah[0], u'dont be them.mp3')
nosklo
+1  A: 

'?' is not valid character for filenames. That is the reason while your approach failed. You may try to use DOS short filenames:

import win32api
filelist = win32api.FindFiles(r'F:/recovery/My Music/*.*')

# this will extract "short names" from WIN32_FIND_DATA structure
filelist = [i[9] if i[9] else i[8] for i in filelist]

# EXAMPLE:    
# this should rename all files in 'filelist' to 1.mp3, 2.mp3, 3.mp3, ...
for (number, filename) in enumerate(filelist):
    os.rename(filaname, '%d.mp3' % (number))
Jiri