2016-08-16 1387 views
0

我寫了一個Python腳本,這是我嘗試從我的服務器自動執行每日ftp傳輸的一部分。我已經測試了一些文件和文件類型(html,mp3,png,jpg等)的腳本,並且一切看起來都很順利。 但是,當我嘗試下載一個簡單的文本文件'file.txt'(9 kb)時,雖然我解釋了文本文件並將其從二進制文件切換到文本模式以進行傳輸,但下載失敗。以下異常被拋出FTPLIB:Python ftplib.error_perm 550:沒有這樣的文件或目錄?

ftplib.error_perm: 550 file.txt: No such file or directory 

這裏是我的腳本:

from ftplib import FTP_TLS, error_perm 
import os 


def open_connection(server, user, pwd, work_dir=None): 
    global ftps 
    try: 
     ftps = FTP_TLS(host=server) 
     ftps.login(user=user, passwd=pwd) 
     ftps.prot_p() # switch to secure data connection 
     if work_dir != None: 
      ftps.cwd(work_dir) 
     else: 
      pass 
    except: 
     pass  


def download_file(remote_path, local_path): 
    remote_file = os.path.basename(remote_path) 
    local_file_path = os.path.join(local_path, remote_file) 

    # differentiate between text and binary files 
    file_type, encoding = guess_type_and_encoding(remote_file) 

    # possibly needs a permission exception catch 
    if file_type.split("/")[0] == "text" and encoding == None: 
     # use text mode for transfer 
     local_file = open(local_file_path, 'w') 
     def callback(line): local_file.write(line + "\n") 
     ftps.retrlines("RETR " + remote_file, callback) 
     local_file.close() 

    else: 
     # use binary mode for transfer 
     local_file = open(local_file_path, 'wb') 
     ftps.retrbinary("RETR " + remote_file, local_file.write) 
     local_file.close() 
    return 


def guess_type_and_encoding(filename): 
    from mimetypes import guess_type, add_type 
    add_type('text/x-python-win', '.pyw') # not in tables 
    mimetype, encoding = guess_type(filename, False) # allow extras 
    mimetype = mimetype or "?/?" # type unknown 
    return mimetype, encoding 


open_connection(server, user, pwd, work_dir) 
download_file("/files/dir/file.txt", "/Users/username/Desktop") 
ftps.close() 

我不明白,爲什麼引發錯誤!?正確提供參數'remote_path'和'local_path'。兩條路都存在! 「file.txt的」存在下的服務器/文件/目錄和/用戶/用戶名/桌面點到我的桌面在OS X上

下面是詳細FTPLIB錯誤:

Traceback (most recent call last): 
    File "ftp2.py", line 138, in <module> 
     download_file("/files/dir/file.txt", "/Users/username/Desktop") 
    File "ftp2.py", line 93, in download_file 
     ftps.retrlines("RETR " + remote_file, callback) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 735, in retrlines 
     conn = self.transfercmd(cmd) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 376, in transfercmd 
     return self.ntransfercmd(cmd, rest)[0] 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 710, in ntransfercmd 
     conn, size = FTP.ntransfercmd(self, cmd, rest) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 339, in ntransfercmd 
     resp = self.sendcmd(cmd) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 249, in sendcmd 
     return self.getresp() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 224, in getresp 
     raise error_perm, resp 
ftplib.error_perm: 550 file.txt: No such file or directory 

任何幫助是很大的讚賞。 謝謝。 :)

回答

1

嘗試 remote_path取代remote_file ftps.retrlines("RETR " + remote_file, callback)

相關問題