从FTP目录打开压缩文件的简便方法

时间:2019-01-07 23:01:19

标签: python python-3.x

我试图找到一种简单直接的方法来下载和打开压缩文件,然后将该文件的内容加载到数据框中。

我有一些代码可以自动登录到FTP目录,并且可以找到要下载的文件。我似乎无法下载并解压缩此文件。如何下载压缩文件,解压缩并将内容加载到数据框中?

user='usr'
password = 'pwd'
ftp = FTP('ftp.corp-site.com', usr, pwd)

locfile = 'C:/Users/ryans/OneDrive/Desktop/' + latest
ftp_path = 'ftp://ftp.corp-site.com/exchangeholidays/' + latest

ftp.retrbinary('RETR ' + ftp_path, locfile.write)

感谢您的光临。

1 个答案:

答案 0 :(得分:0)

我是这样工作的:

target_dir = 'C:/destination/downloads/'
filematch = latest
for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
            ftp.retrbinary('RETR %s' %filename, fhandle.write)