問題描述
我每 6 秒將值附加到日志文件中.每 30 秒,我將此日志作為文件傳輸?shù)?FTP 服務(wù)器.但不是傳輸整個文件,我只想將收集到的數(shù)據(jù)附加到我服務(wù)器上的文件中.我無法弄清楚如何打開服務(wù)器文件然后附加值.
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.
到目前為止我的代碼:
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()
相反,我是否必須下載打開并附加的服務(wù)器文件,然后將其發(fā)回?
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()
推薦答案
使用 APPE
而不是 STOR
.
來源:http://www.gossamer-threads.com/lists/python/python/22960 (鏈接到 web.archive.org)
這篇關(guān)于在 Python 中將數(shù)據(jù)附加到 FTP 服務(wù)器上的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!