os.rename() doesn't work w/unicode?? | | |
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) | | | | re: os.rename() doesn't work w/unicode??
fanbanlo wrote:[color=blue]
> 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[/color]
os.rename works with unicode, you're getting this error because
question marks are not allowed in file names on Windows.
Serge. | | | | re: os.rename() doesn't work w/unicode??
Serge Orlov wrote:[color=blue]
> fanbanlo wrote:
>[color=green]
>>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[/color]
>
>
> os.rename works with unicode, you're getting this error because
> question marks are not allowed in file names on Windows.
>
> Serge.
>
>[/color]
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? | | | | re: os.rename() doesn't work w/unicode??
fanbanlo:
[color=blue]
> 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?[/color]
The convention in the unicode file name support is that calls with
unicode arguments return unicode results:
[color=blue][color=green][color=darkred]
>>> h = "e:\\unicode"
>>> import os
>>> for p,d,f in os.walk(h):[/color][/color][/color]
.... print p, "!", d, "!", f
....
e:\unicode ! [] ! ['ko.py', 'abc', 'ascii', 'Gr\xfc\xdf-Gott', 'uni.py',
'Ge??-sa?', '????????????', '??????', '???', '????G\xdf', '???',
'unilin.py'][color=blue][color=green][color=darkred]
>>> for p,d,f in os.walk(unicode(h, "latin1")):[/color][/color][/color]
.... 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|