【问题标题】:Unzip csv file from Zip on ftp server从 FTP 服务器上的 Zip 解压 csv 文件
【发布时间】: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


【解决方案1】:

固定使用:

ftp.retrlines('RETR %s' % fpath ,lambda s, w=outzip.write: w(s+"\n"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2015-10-21
    相关资源
    最近更新 更多