Created
September 6, 2014 06:23
-
-
Save ReneNyffenegger/20d2ed058d86bdfaeae6 to your computer and use it in GitHub Desktop.
ftp download gz file and unzip it in one go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zlib | |
from ftplib import FTP | |
def get_gz(ftp, ftp_filename, local_filename): | |
decomp = zlib.decompressobj(16+zlib.MAX_WBITS) | |
unzip = open (local_filename, 'wb') | |
def next_packet(data): | |
unzip.write(decomp.decompress(data)) | |
ftp.retrbinary('RETR ' + ftp_filename, next_packet) | |
decompressed = decomp.flush() | |
unzip.write(decompressed) | |
unzip.close() | |
ftp_ = FTP('ftp.host.xyz') | |
ftp_.login() | |
ftp_.cwd('/foo/bar') | |
get_gz(ftp_, 'remote-file.gz', 'local-file') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment