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

F

fanbanlo

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)
 
S

Serge Orlov

fanbanlo said:
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

os.rename works with unicode, you're getting this error because
question marks are not allowed in file names on Windows.

Serge.
 
F

fanbanlo

Serge said:
fanbanlo said:
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


os.rename works with unicode, you're getting this error because
question marks are not allowed in file names on Windows.

Serge.
The filename is supposed to be in Chinese, on NTFS (WinXP). However,
when i print it, they are all '???', and that caused os.rename() to break.

How to force os.walk to return a list of unicode filename?
 
N

Neil Hodgson

fanbanlo:
The filename is supposed to be in Chinese, on NTFS (WinXP). However,
when i print it, they are all '???', and that caused os.rename() to break.

How to force os.walk to return a list of unicode filename?

The convention in the unicode file name support is that calls with
unicode arguments return unicode results:
.... print p, "!", d, "!", f
....
e:\unicode ! [] ! ['ko.py', 'abc', 'ascii', 'Gr\xfc\xdf-Gott', 'uni.py',
'Ge??-sa?', '????????????', '??????', '???', '????G\xdf', '???',
'unilin.py'].... print p, "!", d, "!", f
....
e:\unicode ! [] ! [u'ko.py', u'abc', u'ascii', u'Gr\xfc\xdf-Gott',
u'uni.py', u'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
u'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', u'\u306b\u307d\u3093',
u'\u66e8\u05e9\u3093\u0434\u0393\xdf', u'\u66e8\u66e9\u66eb', u'unilin.py']

Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,120
Members
47,741
Latest member
WilliamsFo

Latest Threads

Top