Copying files with unicode names

前端 未结 1 634
北荒
北荒 2021-01-14 18:36

this was supposed to be a simple script

import shutil

files = os.listdir(\"C:\\\\\")
for efile in files:
    shutil.copy(efile, \"D:\\\\\")
<
相关标签:
1条回答
  • 2021-01-14 19:17

    You need to use Unicode to access filenames which aren't in the ACP (ANSI codepage) of the Windows system you're running on. To do that, make sure you name directories as Unicode:

    import shutil
    
    files = os.listdir(u"C:\\")
    for efile in files:
        shutil.copy(efile, u"D:\\")
    

    Passing a Unicode string to os.listdir will make it return results as Unicode strings rather than encoding them.

    Don't forget that os.listdir won't include path, so you probably actually want something like:

    shutil.copy(u"C:\\" + efile, u"D:\\")
    

    See also http://docs.python.org/howto/unicode.html#unicode-filenames.

    0 讨论(0)
提交回复
热议问题