Append data to file on FTP server in Python(在 Python 中将数据附加到 FTP 服务器上的文件)
问题描述
我每 6 秒将值附加到日志文件中.每 30 秒,我将此日志作为文件传输到 FTP 服务器.但不是传输整个文件,我只想将收集到的数据附加到我服务器上的文件中.我无法弄清楚如何打开服务器文件然后附加值.
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()
相反,我是否必须下载打开并附加的服务器文件,然后将其发回?
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)
这篇关于在 Python 中将数据附加到 FTP 服务器上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中将数据附加到 FTP 服务器上的文件


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01