我想检索存储在 FTP 服务器上的压缩 gz 文件中的数据,而不将文件写入本地存档.
I would like to retrieve the data inside a compressed gz file stored on an FTP server, without writing the file to the local archive.
目前我已经完成了
from ftplib import FTP
import gzip
ftp = FTP('ftp.server.com')
ftp.login()
ftp.cwd('/a/folder/')
fileName = 'aFile.gz'
localfile = open(fileName,'wb')
ftp.retrbinary('RETR '+fileName, localfile.write, 1024)
f = gzip.open(localfile,'rb')
data = f.read()
然而,这会将文件localfile"写入当前存储.
This, however, writes the file "localfile" on the current storage.
我试图改变这个
from ftplib import FTP
import zlib
ftp = FTP('ftp.server.com')
ftp.login()
ftp.cwd('/a/folder/')
fileName = 'aFile.gz'
data = ftp.retrbinary('RETR '+fileName, zlib.decompress, 1024)
但是,ftp.retrbinary 不输出其回调的输出.有没有办法做到这一点?
but, ftp.retrbinary does not output the output of its callback.
Is there a way to do this?
一个简单的实现是:
将文件下载到内存中类似文件的对象,例如 BytesIO;
将其传递给 fileobj 参数noreferrer">GzipFile 构造函数.
pass that to fileobj parameter of GzipFile constructor.
import gzip
from io import BytesIO
import shutil
from ftplib import FTP
ftp = FTP('ftp.example.com')
ftp.login('username', 'password')
flo = BytesIO()
ftp.retrbinary('RETR /remote/path/archive.tar.gz', flo.write)
flo.seek(0)
with open('archive.tar', 'wb') as fout, gzip.GzipFile(fileobj = flo) as gzip:
shutil.copyfileobj(gzip, fout)
<小时>
以上将整个 .gz 文件加载到内存中.对于大文件来说什么是低效的.更智能的实现将改为流式传输数据.但这可能需要实现一个智能的自定义类文件对象.
The above loads whole .gz file to a memory. What can be inefficient for large files. A smarter implementation would stream the data instead. But that would probably require implementing a smart custom file-like object.
另请参阅在 FTP 服务器上的 zip 文件中获取文件名,而无需下载整个存档.
这篇关于从 FTP 服务器上的 gz 文件中检索数据而不在本地写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么我不能插入 Python 列表?Why I cannot make an insert to Python list?(为什么我不能插入 Python 列表?)
在 DataFrame 的开头(最左端)插入一列Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的开头(最左端)插入一列)
Python psycopg2 没有插入到 postgresql 表中Python psycopg2 not inserting into postgresql table(Python psycopg2 没有插入到 postgresql 表中)
list extend() 索引,不仅将列表元素插入到末尾list extend() to index, inserting list elements not only to the end(list extend() 索引,不仅将列表元素插入到末尾)
如何使用 list.insert 将 Python 中的元素添加到列表How to add element in Python to the end of list using list.insert?(如何使用 list.insert 将 Python 中的元素添加到列表末尾?)
TypeError:“浮动"对象不可下标TypeError: #39;float#39; object is not subscriptable(TypeError:“浮动对象不可下标)