Python, add data to file on server how?

南楼画角 提交于 2019-12-01 06:32:26

问题


I'm appending values to a log file every 6th second. Every 30 sec I'm transferring this log to a 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? Don't ask why I'm not sending the logged data straight to a DB ;)

ABOVE THIS ROW WAS MY QUESTION, FULL SOLUTION BELOW

session.cwd("//"+serverCatalog()+"//") # open server catalog
localfile = open("logfile.txt",'rb')
session.storbinary('APPE serverfile.txt', localfile)
localfile.close()

回答1:


Try using APPE instead of STOR. Source: http://www.gossamer-threads.com/lists/python/python/22960



来源:https://stackoverflow.com/questions/30103662/python-add-data-to-file-on-server-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!