os.rename() doesn't work w/unicode??

fanbanlo henryho at NO__SPAMM_.telus.net
Tue Feb 15 00:09:08 EST 2005


C:\MP3\001.txt -> 0.txt
C:\MP3\01. ??? - ????(???).mp3 -> 1.mp3

Traceback (most recent call last):
   File 
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", 
line 310, in RunScript
     exec codeObject in __main__.__dict__
   File "C:\MP3\!RenameNum.py", line 40, in ?
     renameFiles(os.path.dirname(sys.argv[0]))
   File "C:\MP3\!RenameNum.py", line 26, in renameFiles
     os.rename(os.path.join(path, filenames), new_filename)
OSError: [Errno 22] Invalid argument

-----

def renameFiles(folder):
     """
         The function called for each directory visited.
         We'll rename all the files in consecutive number except
         files with filename begins with '!'

     """

     file_num_counter = 0

     for path, dirs, files in os.walk(folder):
         for filenames in files:
             if filenames.startswith('!'):
                 print 'file: ' + filenames + ' is ignored!'
             else:
                 file_extension  = filenames.split('.')[-1]
                 new_filename = str(file_num_counter) + '.' \
		+ file_extension
                 file_num_counter += 1

                 print os.path.join(path, filenames), "->", new_filename
                 os.rename(os.path.join(path, filenames), new_filename)



More information about the Python-list mailing list