4

My problem is similar to Python's 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?

Community
  • 1
  • 1
  • 1
    You say that the repr of blah[0] is exactly `Don?t Be Them.mp3` (without the backquotes), and I say you didn't copy-paste, but improvised. – tzot May 11 '09 at 13:32
  • Are both the filesystems NTFS? Could it be that you copy to a local FAT32 volume? Did you attach locally the failed disk, or did you copy over network/CIFS? – tzot May 11 '09 at 13:34

2 Answers2

2

'?' 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
  • 15,625
  • 5
  • 49
  • 67
0

Try passing a unicode string:

os.rename(blah[0], u'dont be them.mp3')
nosklo
  • 193,422
  • 54
  • 273
  • 281