Reputation: 25
I want to move the files to a specific folder.
However, due to a problem with Unicode, the file is not being moved.
import os
import shutil
file=r"c:/test/test.rar"
folder=r"c:/test/発射"
shutil.move(file, folder)
#os.rename(file, os.path.join(folder, os.path.split(file)[1]))
"shutil.move" and "os.rename" return "[Errno 22] invalid mode ('wb') or filename" and "WindowsError: [Error 123] file name". They do not recognize Unicode filename.
I searched for many solutions, but I could not solve them.
Can this be solved?
Thank you in advance for your help.
Upvotes: 0
Views: 644
Reputation: 148965
Since NT 3 version, Windows has natively allowed unicode file names provided characters are in the Basic Multilingual Plane (unicode code less or equal to U+FFFF). Simply, you should add a '/' to the folder name. So this should work in Python 3 if the folder exists:
file="c:/test/test.rar"
folder="c:/test/\u767a\u5c04/"
shutil.move(file, folder)
I forced the unicode code for the non ascii characters, because an editor could use a wrong encoding
Upvotes: 0
Reputation: 2427
Try to encode folder name with system's encoding:
import sys
folder = r"c:/test/発射".encode(sys.getfilesystemencoding())
Also, if you wants to use os.rename
I recommend you to rewrite your line in such manner:
os.rename(file, os.path.join(folder, os.path.basename(file)))
Upvotes: 1