[英]Python ftp download and archive
I have been trying to script a code with python to grade the main directory of that ftp and archive it into a the local pc. 我一直在尝试用python编写代码脚本,以对该ftp的主目录进行分级并将其归档到本地PC中。 I am not an amateur coder and python is fairly new to me.
我不是业余编码员,而python对我来说还很新。 What I am getting as an error right now is.
我现在正得到一个错误。
File "C:\Users\Ali\Desktop\ftp_archiving_script.py", line 24, in <module>
ftpDownload = ftp.retrbinary('RETR', filename)
Code: 码:
from ftplib import FTP
import zipfile
import os
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
modes = { zipfile.ZIP_DEFLATED: "deflated",
zipfile.ZIP_STORED: "stored",
}
#print "Logging in..."
with FTP('xxx.xxx.xxx') as ftp: #hostname goes here
ftp.login('xxxx','xxxx') #user followed by pass
#print "changing to root directory"
ftp.mlsd('//')
#print "Accessing files"
filenames = []
#print filenames
ftp.retrlines('NLST', filenames.append)
try:
for filename in filenames:
ftpDownload = ftp.retrbinary('RETR', filename)
with ZipFile(os.path.join('C:\\','DC_archive.zip'), 'w') as myzip:
myzip.write(ftpDownload, compress_type=compression)
myzip.close()
finally:
#print "closing"
ftp.close()
ftp.quit()
Can anyone enlighten me on this problem. 谁能启发我这个问题。
Thank you, 谢谢,
Update 更新
try:
for filename in filenames:
with io.StringIO() as fObject:
ftp.retrbinary('RETR %s' %filename, fObject.write)
with ZipFile(os.path.join('C:\\','DC_archive.zip'), 'w') as myzip:
myzip.write(fObject, compress_type=compression)
myzip.close()
updated Traceback for @fals... Also this is using your code below and not the one I have at the top. 更新了@fals的Traceback ...这也在下面使用您的代码,而不是我在顶部使用的代码。
Traceback (most recent call last):
File "C:\Users\Ali\Desktop\ftp_archive2.py", line 20, in <module>
ftpDownload = ftp.retrbinary('RETR ' + filename, f.write)
File "C:\Python33\lib\ftplib.py", line 424, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File "C:\Python33\lib\ftplib.py", line 386, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 352, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python33\lib\ftplib.py", line 259, in sendcmd
return self.getresp()
File "C:\Python33\lib\ftplib.py", line 233, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 File not found
From the Python documentation for ftplib.retrbinary : 从ftplib.retrbinary的Python文档中:
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
FTP.retrbinary(command,callback [,maxblocksize [,rest]])
Retrieve a file in binary transfer mode.
以二进制传输模式检索文件。 command should be an appropriate RETR command: 'RETR filename'.
该命令应为适当的RETR命令:“ RETR文件名”。 The callback function is called for each block of data received, with a single string argument giving the data block.
对于每个接收到的数据块,将调用回调函数,并使用单个字符串参数指定该数据块。
Nowhere does it indicate that it returns a file-like object or string. 它无处表明它返回了类似文件的对象或字符串。
Instead, you have to create your own callback to write to a file object. 相反,您必须创建自己的回调以写入文件对象。
with open('my-downloaded-file', 'wb') as f:
ftp.retrbinary('RETR %s' % filename, f.write)
Here, f.write
is the callback which will receive data as it arrives from the socket. 在这里,
f.write
是回调,它将接收来自套接字的数据。 If you don't want to save the file to disk using open
, you can use the StringIO module to simulate a file in memory. 如果您不想使用
open
将文件保存到磁盘,则可以使用StringIO模块在内存中模拟文件。
Try following code: 尝试以下代码:
import ftplib
from io import BytesIO
import os
import zipfile
REMOTE_HOST = 'xxx.xxx.xxx'
REMOTE_USER = '...'
REMOTE_PASS = '...'
REMOTE_DIR_PATH = '//'
LOCAL_ZIP_PATH = os.path.join(os.path.expanduser('~'), 'Desktop', 'DC_archive.zip')
ftp = ftplib.FTP(REMOTE_HOST)
try:
ftp.login(REMOTE_USER, REMOTE_PASS)
ftp.cwd(REMOTE_DIR_PATH)
filenames = ftp.nlst()
with zipfile.ZipFile(LOCAL_ZIP_PATH, 'w') as zf:
for filename in filenames:
with BytesIO() as f:
try:
ftpDownload = ftp.retrbinary('RETR ' + filename, f.write)
zf.writestr(filename, f.getvalue())
except ftplib.Error as e:
print('Skip {}: {}'.format(filename, e))
finally:
ftp.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.