【发布时间】:2016-12-09 17:41:41
【问题描述】:
我想登录 ftp 服务器(不是公共 url)并下载 csv 文件,该文件位于 zip 文件中,然后将其保存到特定目录:
#log in OK
# this is the zip file I want to download
fpath = strDate + ".zip"
#set where to save file
ExtDir = "A:\\LOCAL\\DIREC\\TORY\\"""
ExtDir = ExtDir + strdate + "\\"
ExtFile = ExtDir + "Download.zip"
#download files
#use zipfile.ZipFile as alternative method to open(ExtFile, 'w')
with zipfile.ZipFile(ExtFile,'w') as outzip:
ftp.retrbinary('RETR %s' % fpath , outzip.write)
outzip.close
我收到此错误
文件“C:\Program Files (x86)\Python 2.7\lib\ftplib.py”,第 419 行,在 retrbinary 回调(数据)中 文件“C:\Program Files (x86)\Python 2.7\lib\zipfile.py”,第 1123 行,写入 st = os.stat(filename) TypeError: stat() 参数 1 必须是没有空字节的编码字符串,而不是 str
【问题讨论】:
-
你可以试试
open(ExtFile, 'wb')吗?见stackoverflow.com/a/2665873 -
它只是给我一个错误“IOError:[Errno 2]没有这样的文件或目录”因为这个'wb'不会生成文件。如果我使用
open(ExtFile, 'w')然后关闭它,我会创建一个 zip 文件但我无法打开它。
标签: python-2.7 ftp zip