Python ftplib.error_perm 550:没有这样的文件或目录?
我已经编写了一个Python脚本,这是我尝试从服务器自动化每日ftp传输的一部分。我已经用许多文件和文件类型(html、mp3、png、jpg等)测试了脚本,到目前为止,一切似乎都很好。 但是,当我尝试下载一个简单的文本文件“file.txt”(9KB)时,下载失败,尽管我考虑了文本文件并从二进制模式切换到文本模式进行传输。ftplib引发以下异常:Python ftplib.error_perm 550:没有这样的文件或目录?,python,python-2.7,ftplib,Python,Python 2.7,Ftplib,我已经编写了一个Python脚本,这是我尝试从服务器自动化每日ftp传输的一部分。我已经用许多文件和文件类型(html、mp3、png、jpg等)测试了脚本,到目前为止,一切似乎都很好。 但是,当我尝试下载一个简单的文本文件“file.txt”(9KB)时,下载失败,尽管我考虑了文本文件并从二进制模式切换到文本模式进行传输。ftplib引发以下异常: ftplib.error_perm: 550 file.txt: No such file or directory 这是我的剧本: from
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()
我不明白为什么会出现错误!?正确提供了参数“远程路径”和“本地路径”。两条路都存在服务器上的/files/dir下存在file.txt,并且/Users/username/Desktop指向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
回溯(最近一次呼叫最后一次):
文件“ftp2.py”,第138行,在
下载文件(“/files/dir/file.txt”,“/Users/username/Desktop”)
下载文件中第93行的文件“ftp2.py”
ftps.retrline(“RETR”+远程_文件,回调)
retrlines中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第735行
conn=self.transfercmd(cmd)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第376行,在transfercmd中
返回self.ntransfercmd(cmd,rest)[0]
ntransfercmd中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第710行
conn,size=FTP.ntransfercmd(self,cmd,rest)
ntransfercmd中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第339行
resp=self.sendcmd(cmd)
sendcmd中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第249行
返回self.getresp()
getresp中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py”,第224行
引发错误\u perm,resp
ftplib.error\u perm:550 file.txt:没有这样的文件或目录
非常感谢您的帮助。
谢谢。:) 试着
替换远程文件
在ftps.retrlines(“RETR”+远程文件,回调)
带有远程路径