我创建了这个脚本,用于将数据记录到python (raspbian与Arduino & DHT-11一起用于尝试python中的串口),并显示它,并在每一行上加上时间戳记录它,并将传感器数据附加到文本文件中。问题是,我希望这个文本文件位于这个python编辑器之外的其他目录中。提前感谢
当前,这个程序将weather_data.txt输出到/home/pi/mu_code,我希望它将文本文件输出到/home/pi/shared/
x = (f"{str(localtime().tm_mday)} - {str(localtime().tm_mon)} - {str(localtime().tm_year)}")
with open(x + ".txt", "a") as weather: #making/opening the .txt file
print(strftime("%d-%m-%Y %I:%M:%S %p", localtime()) ,end="", file=weather) #adding timestamp to file
print(f" :: T = {t}°C - rh = {rh}%", file=weather) #adding data to file我需要使用open()命令在“/home/pi/shared/ *.txt”中创建一个,而不是“/home/pi/mu”
发布于 2019-08-11 12:29:10
经过一些研究得到了解决方案。我们可以将路径添加为字符串,并将其附加到文件名中,即:
我们可以要求open()创建/home/pi/shared/weather.txt,而不是要求创建weather.txt,它将在/home/pi/shared/文件夹中创建文件。
https://stackoverflow.com/questions/57448812
复制相似问题