Python : Downloaded tar.gz files over FTP cannot be opened

Viewed 25

I'd like to download tar.gz files over FTP. It uses FTP_TLS. Files are downloaded but when I try to open them on Windows (I use 7zip), it's not working.

Error is :

7zip eror message

"File [...] cannot be opened as an archive"

This is my code (It needs improvment I know I'm quite newbee :) ):

def get_ftp(ip, login, passwd, path):
""" Connexion FTP """
try:
    with ftplib.FTP_TLS(ip, login, passwd) as ftps:
        ftps.prot_p()
        # ftp.login(login, passwd)
        files = ftps.nlst('/home/user/dir/' + path)
        # ftp.retrlines('LIST')

        if files:
            for file in files:
                if file.endswith('.tar.gz'):
                    if file + '.md5' in files:
                        localfile = join(path_recu, basename(file))
                        with open(localfile, 'wb') as binary_file:
                            response = ftps.retrbinary('RETR %s' % file, binary_file.write, blocksize=8192, rest=None)
                            if response.startswith('226'):
                                with open(localfile, 'w') as text_file:
                                    ftps.retrlines('RETR %s' % file + '.md5', text_file.write)

except ftplib.error_perm as resp:
    if str(resp):
        logger.critical('ERREUR : ' + repr(resp))
        raise
else:
    return files

I've tried with "blocksize=4096": same error.

Any ideas ?

0 Answers
Related