Python,如何在服务器上向文件添加数据? [英] Python, add data to file on server how?

查看:331
本文介绍了Python,如何在服务器上向文件添加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每6秒钟将值附加到一个日志文件中。我每30秒将此日志作为文件传输到服务器。但是,我只想将收集的数据附加到服务器上的文件中,而不是传输整个文件。我还无法弄清楚如何打开服务器文件然后附加值。
我到目前为止的代码:

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()


推荐答案

尝试使用 APPE 代替 STOR 。来源: http://www.gossamer-threads.com/lists/python/python / 22960

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

这篇关于Python,如何在服务器上向文件添加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆