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

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 :
"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 ?

Related

Possibilities to move file one folder to another on remte FTP server using python [duplicate]

I am trying to move some XML files in an FTP location to another location in the same FTP. I tried with the following code, but it doesn't work.
def ftpPush(filepathSource, filename, filepathDestination):
try:
ftp = FTP(ip, username, password)
ftp.cwd(filepathDestination)
ftp.storlines("STOR "+filename, open(filepathSource, 'r'))
ftp.quit()
for fileName in os.listdir(path):
if fileName.endswith(".xml"):
ftpPush(filepathSource, filename, filepathDestination)
except Exception, e:
print str(e)
finally:
ftp.close()
To move a file use the FTP.rename.
Assuming that the filepathSource and the filepathDestination are both remote files, you do:
ftp.rename(filepathSource, filepathDestination)

Python ftplib add new line to txt file during upload file to FTP server

I use following code to upload txt files in ASCII mode to FTP server
import glob
import os
import hashlib
from ftplib import FTP
server = '1.1.1.1'
login = 'user'
password = 'password'
path = './test_files/'
file_mask = '*.txt'
def upload_to_ftp(srv, uname, pwd, file_name):
ftp = FTP(srv, uname, pwd)
ftp.cwd('Pava')
file = open(path+file_name, 'rb')
ftp.storlines('STOR '+file_name, file)
size = ftp.size(file_name)
ftp.close()
file.close()
print (size)
def local_size_check(file_name):
file_size = os.stat(path+file_name)
print (file_size.st_size)
file_to_upload = glob.glob1(path, file_mask)
for i in file_to_upload:
try:
os.rename(path+i, path+i)
except OSError as e:
print ('Access-error on file ' + i + ' ! \n' + str(e))
else:
upload_to_ftp(server, login, password, i)
local_size_check(i)
The output of this two functions is:
78
76
Then i have dowloaded file from ftp and found that during transfering by FTP was added new line at the end of file.
local and remote file screens
Please help to solve this problem.
BTW if use binary mode new line do not add
You should upload your file in binary mode so that it will not be subject to the server's text interpretation.
Change:
ftp.storlines('STOR '+file_name, file)
to:
ftp.storbinary('STOR '+file_name, file)

Problems downloading files from ftp server to directory

I am trying to download files from an ftp server to a local folder. Below I have included my code, the script is able to access the ftp succesfully and list the names of the files on the ftp server however where my issue lies is I am not sure how to download them to a local directory.
from ftplib import FTP
import os, sys, os.path
def handleDownload(block):
file.write(block)
ddir='U:/Test Folder'
os.chdir(ddir)
ftp = FTP('sidads.colorado.edu')
ftp.login()
print ('Logging in.')
directory = '/pub/DATASETS/NOAA/G02158/unmasked/2012/04_Apr/'
print ('Changing to ' + directory)
ftp.cwd(directory)
ftp.retrlines('LIST')
print ('Accessing files')
filenames = ftp.nlst() # get filenames within the directory
print (filenames)
I tried using the following code below however it gives me the error in the photo below.
for filename in filenames:
if filename != '.':
local_filename = os.path.join('U:/Test Folder/', filename)
file = open(local_filename, 'wb')
ftp.retrbinary('RETR '+ filename, file.write)
file.close()
ftp.quit()
Try the following:
for filename in filenames:
if filename not in ['.', '..']:
local_filename = os.path.join(ddir, filename)
with open(local_filename, 'wb') as f_output:
ftp.retrbinary('RETR '+ filename, f_output.write)
ftp.quit()
Your code was still attempting to download . filenames. You need to indent the writing of the valid filenames. Also by using with it will close the file automatically.

Dropbox API v2 - uploading files

I'm trying to loop through a folder structure in python and upload each file it finds to a specified folder. The problem is that it's uploading a file with the correct name, however there is no content and the file size is only 10 bytes.
import dropbox, sys, os
try:
dbx = dropbox.Dropbox('some_access_token')
user = dbx.users_get_current_account()
except:
print ("Negative, Ghostrider")
sys.exit()
rootdir = os.getcwd()
print ("Attempting to upload...")
for subdir, dirs, files in os.walk(rootdir):
for file in files:
try:
dbx.files_upload("afolder",'/bfolder/' + file, mute=True)
print("Uploaded " + file)
except:
print("Failed to upload " + file)
print("Finished upload.")
Your call to dbx.files_upload("afolder",'/bfolder/' + file, mute=True) says: "Send the text afolder and write it as a file named '/bfolder/' + file".
From doc:
files_upload(f, path, mode=WriteMode('add', None), autorename=False, client_modified=None, mute=False)
Create a new file with the contents provided in the request.
Parameters:
f – A string or file-like obj of data.
path (str) – Path in the user’s Dropbox to save the file.
....
Meaning that f must be the content of the file (and not the filename string).
Here is a working example:
import dropbox, sys, os
dbx = dropbox.Dropbox('token')
rootdir = '/tmp/test'
print ("Attempting to upload...")
# walk return first the current folder that it walk, then tuples of dirs and files not "subdir, dirs, files"
for dir, dirs, files in os.walk(rootdir):
for file in files:
try:
file_path = os.path.join(dir, file)
dest_path = os.path.join('/test', file)
print 'Uploading %s to %s' % (file_path, dest_path)
with open(file_path) as f:
dbx.files_upload(f, dest_path, mute=True)
except Exception as err:
print("Failed to upload %s\n%s" % (file, err))
print("Finished upload.")
EDIT: For Python3 the following should be used:
dbx.files_upload(f.read(), dest_path, mute=True)
For Dropbox Business API below python code helps uploading files to dropbox.
#function code
import dropbox
def dropbox_file_upload(access_token,dropbox_file_path,local_file_name):
'''
The function upload file to dropbox.
Parameters:
access_token(str): Access token to authinticate dropbox
dropbox_file_path(str): dropboth file path along with file name
Eg: '/ab/Input/f_name.xlsx'
local_file_name(str): local file name with path from where file needs to be uploaded
Eg: 'f_name.xlsx' # if working directory
Returns:
Boolean:
True on successful upload
False on unsuccessful upload
'''
try:
dbx = dropbox.DropboxTeam(access_token)
# get the team member id for common user
members = dbx.team_members_list()
for i in range(0,len(members.members)):
if members.members[i].profile.name.display_name == logged_in_user:
member_id = members.members[i].profile.team_member_id
break
# connect to dropbox with member id
dbx = dropbox.DropboxTeam(access_token).as_user(member_id)
# upload local file to dropbox
f = open(local_file_name, 'rb')
dbx.files_upload(f.read(),dropbox_file_path)
return True
except Exception as e:
print(e)
return False

Delete files from ftp after download

I am newbie to python and learning to delete the file which are downloaded from ftp site.
this is my error :OSError: [Errno 2] No such file or directory: 'RingGoData-2014-07-02.csv'
this is my code:
ftp = ftplib.FTP('192.198.0.20', 'bingo', 'Password')
files = ftp.dir('/')
ftp.cwd("/")
#ftp.retrlines('LIST')
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os
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)
if os.path.isdir(filename)== True:
shutil.rmtree(filename)
else:
os.remove(filename)
Shutil and os.remove work on your current filesystem, NOT on the FTP server. Your trying to delete a local file which isn't there.
You should use FTP.delete(filename)

Categories

Resources