python - 将Python中的数据写入本地文件并同时上传到FTP不起作用
问题描述
我在 Raspberry Pi 4 上的代码有这个奇怪的问题。
from gpiozero import CPUTemperature
from datetime import datetime
import ftplib
cpu = CPUTemperature()
now = datetime.now()
time = now.strftime('%H:%M:%S')
# Save data to file
f = open('/home/pi/temp/temp.txt', 'a+')
f.write(str(time) + ' - Temperature is: ' + str(cpu.temperature) + ' C\n')
# Login and store file to FTP server
ftp = ftplib.FTP('10.0.0.2', 'username', 'pass')
ftp.cwd('AiDisk_a1/usb/temperature_logs')
ftp.storbinary('STOR temp.txt', f)
# Close file and connection
ftp.close()
f.close()
当我有此代码时,脚本不会向 .txt 文件写入任何内容,并且传输到 FTP 服务器的文件的大小为 0 字节。
当我删除这部分代码时,脚本正在写入文件就好了。
# Login and store file to FTP server
ftp = ftplib.FTP('10.0.0.2', 'username', 'pass')
ftp.cwd('AiDisk_a1/usb/temperature_logs')
ftp.storbinary('STOR temp.txt', f)
...
ftp.close()
我还尝试将一些随机文本写入文件并运行脚本,文件传输正常。
你有什么想法,我错过了什么?
解决方案
推荐阅读
- angular - How can I set Angular to reload only when I save
- typescript - 如何将生命周期策略添加到 AWS CDK Typescript 中的现有 S3 存储桶
- python - Python,Seaborn 使用 xtick.rotation 设置默认样式
- c++ - 你能用mysql服务器和连接器c++定位x86吗
- swift - 如何在 SwiftUI 中使用 CustomType 进行绑定,使我的视图停止不必要的渲染?
- pandas - 根据每行的列表值对 Pandas 数据帧进行排序
- python - 我正在尝试为学校项目创建二进制搜索程序,但某些数字会导致无限递归
- node.js - 为什么这个对 Ingress (GKE) 的 HTTP GET 请求总是返回 400(错误请求)错误?
- prestashop - Prestashop 1.7 白页
- php - Laravel 重定向离开方法重定向到自己的域