python----ftplib中遇到中文路徑錯誤問題


python----ftplib中遇到中文路徑錯誤問題

筆者在寫一個簡易的ftp程序的時候。

遇到返回提示說找不到FTP上的路徑。

但是路徑肯定時沒錯的。

而且當路徑變成普通的不含中文的路徑的時候,就是正常的。

下面是筆者的代碼

#!/usr/bin/python3
#-*- coding: utf-8 -*-
from ftplib import FTP
import sys,time,os,hashlib

#定義時間
sys_time = time.time()
sys_time_array = time.localtime(sys_time)
current_time = time.strftime("%Y-%m-%d %H:%M:%S:",sys_time_array)

class ftp():
    def __init__(self,ip,port,user,password):
        self.ip = ip
        self.port = port
        self.user = user
        self.password = password

    #----------------定義下載模塊-----------------#
    def ftp_download(self,remote_path,local_path):
        ftp = FTP()
        try:
            ftp.connect(self.ip,self.port)
            ftp.login(self.user,self.password)
        except:
            print('connect to FTP server failed!!!')
        else:
            file_list = ftp.nlst(remote_path)
            key = os.path.exists(local_path)
            if str(key) == 'True':
                pass
            else:
                os.makedirs(local_path)
            print("downloading!!!")
            try:
                for file in file_list:
                    bufsize = 1024
                    file_name = file.split('/')[-1]
                    local_file = open(local_path+file_name,'wb')
                    ftp.retrbinary('RETR %s'%(file),local_file.write,bufsize)
                    ftp.set_debuglevel(0)
                    local_file.close()
            except:
                print("%s %s download failed!!!" %(current_time,remote_path))
            else:
                print("%s %s download successfully!!!" %(current_time,remote_path))

    #----------------定義上傳模塊-----------------#
    def ftp_upload(self,remote_path,local_path):
        ftp = FTP()
        try:
            ftp.connect(self.ip,self.port)
            ftp.login(self.user,self.password)
        except:
            print('connect to FTP server failed!!!')
        else:
            try:
                ftp.mkd(remote_path)
            except:
                pass
        try:
            file_list = os.walk(local_path)
            for root,dirs,files in file_list:
                for file in files:
                    local_file = local_path + file
                    remote_file = remote_path + file
                    bufsize = 1024
                    fp = open(local_file,'rb')
                    ftp.storbinary('STOR ' + remote_file, fp, bufsize)
                    fp.close()
        except:
            print("%s %s upload failed!!!" %(current_time,local_path))
        else:
            print("%s %s upload successfully!!!" %(current_time,local_path))

 

查閱了很多網上的資料,發現在python自帶的模塊ftplib.py中定義了編碼模式

vim /usr/local/python3/lib/python3.6/ftplib.py

初始的編碼模式是

coding = 'latin-1'

后來筆者把他改成了‘utf-8’

但是問題並不能解決

最后筆者狠下心來把他改成了

encoding = "GB2312"

問題迎刃而解

 

這里的重點應該是了解FTP服務器究竟是搭建在什么機子上,然后需要把ftplib.py中的編碼模式改成對應的編碼模式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM