Python Forum

Full Version: upload Files to FTP and file name is Chinese
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

i have a file folder to be uploaded to FTP, but some file names are Simple Chinese, some are traditional Chinese.
Currently,traditional Chinese file names could be uploaded successfully,but Simple Chinese failed.

i define functions as follows
def dirOrflie(name,localpath):
    if os.path.isfile(localpath):
        print("STOR", name, localpath)
        ftp.storbinary('STOR ' + name, open(localpath,'rb'))
    elif os.path.isdir(localpath):
        print("MKD", name)

        try:
            ftp.mkd(name)

        # ignore "directory already exists"
        except error_perm as e:
            if not e.args[0].startswith('550'): 
                raise

            print("CWD", name)
            ftp.cwd(name)
            placeFiles(ftp, localpath)           
            print("CWD", "..")
            ftp.cwd("..")

def placeFiles(ftp, path):
    for name in os.listdir(path):
        print('name='+name)
        localpath = os.path.join(path, name)
        print('localpath='+localpath)
        
        try:
            print('1')
            ftp.encoding='big5'
            print('2')
            dirOrflie(name,localpath)
            print('3')
        except Exception as e:
            print('4')
            ftp.encoding='gbk'
            dirOrflie(name,localpath)
            

try:
    placeFiles(ftp, filenameCV)
    print('successfully')
except Exception as e:
    print('exception'+str(e))
    print('exception bye')
    ftp.quit()          
i tried utf-8, but error message as follows, and traditional Chinese file names also failed
error message:
name=测试档案.xlsx
localpath=C:\Users\User\Desktop\test\测试档案.xlsx
1
2
STOR 测试档案.xlsx C:\Users\User\Desktop\test\测试档案.xlsx
4
STOR 测试档案.xlsx C:\Users\User\Desktop\test\测试档案.xlsx
exception426 Connection closed; file opening failed (ÀɮצWºÙ¡B¥Ø¿ý¦WºÙ©ÎºÏºÐ°Ï¼ÐÅÒ»yªk¿ù»~¡C).
exception bye


i try the code as above, simple Chinese file names are decoded error, for example, local file name is 测试档案.xlsx,but file name uploaded to FTP becomes 聆彸紫偶.xlsx. i tried GBK,GB2312,but have encountered the same problem

Does someone have ideas?
Thanks very much.
when I try to translate 测试档案.xlsx I get 'Test file .xlsx' with spaces, it that correct?
The uploaded file name must be the same as the local file name.