开发者

How to rename files containing Korean characters in python

I want to rename more than 100 files in windows 7, all of them have korean/hangul characters. But os.ren开发者_高级运维ame() doesn't work with these files.

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect

I'm using python 2.6.5 and os.listdir() for getting the filenames which gives me something like ???? ??? 021? 061205


Which version of Python?

What is your locale?

You are using os.rename(src, dst) ... what does print repr(src) give you for a typical file? Use print(ascii(src)) with Python 3.x.

How are you getting src for each file? If it is some kind of "get me all the files in some-folder" gadget, have you supplied some-folder as a unicode object?

Update: If in fact you are doing something like:

for filename in os.listdir("C:\\foo\\bar\\baz"):
    dst = some_func(filename)
    os.rename(filename, dst)

you may like to do as indicated by the os.listdir docs and my earlier hint, and try this:

for filename in os.listdir(u"C:\\foo\\bar\\baz"):
    dst = some_func(filename)
    os.rename(filename, dst)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜