Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append data to file on FTP server in Python

I'm appending values to a log file every 6th second. Every 30 sec I'm transferring this log to an FTP server as a file. But instead of transfering the whole file, I just want to append the collected data to the file on my server. I haven't been able to figure out how to open the server file and then append the values.

My code so far:

session = ftplib.FTP(authData[0],authData[1],authData[2])
session.cwd("//"+serverCatalog()+"//") # open server catalog
file = open(fileName(),'rb')

with open(fileName(), 'rb') as f:
     f = f.readlines()
         for line in f:
             collected = line
           
           # In some way open server file, write lines to it
           session.storbinary('STOR ' + fileName(), open(fileName(), 'a'), 1024)
           file.close()
           session.quit()

Instead, do I have to download the server file open and append, then send it back?


Above was my question, the full solution is below:

session.cwd("//"+serverCatalog()+"//") # open server catalog
localfile = open("logfile.txt",'rb')
session.storbinary('APPE serverfile.txt', localfile)
localfile.close()
like image 645
Christian Avatar asked May 07 '15 14:05

Christian


1 Answers

Use APPE instead of STOR.

Source: http://www.gossamer-threads.com/lists/python/python/22960 (link to web.archive.org)

like image 69
lmz Avatar answered Sep 28 '22 00:09

lmz