gpt4 book ai didi

python - os.rename()错误不存在这样的文件或文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:18 27 4
gpt4 key购买 nike

我一直在尝试创建一个 python 脚本,将我的数据记录到一个名为“log.dat”的 .dat 文件中,并且每分钟将 log.dat 重命名为其他名称,然后开始将传入的日志数据写入一个新的,空的 log.dat 文件。

但是,我的 os.rename 行创建了一个错误,我已经尝试调试它很长时间了,但它没有帮助。我不断收到同样的错误提示

Error : Traceback (most recent call last):rov sel.:0; homenet:0(-1); current net:0;
File "tracer.py", line 56, in <module>
main()
File "tracer.py", line 44, in main
os.rename("/home/debian/fname", "/home/debian/log-{}.dat".format(time.strftime("%y%m%d%H%M%S")))
OSError: [Errno 2] No such file or directory

这是我的代码:

from __future__ import print_function
def main():


#!/usr/bin/python
# get lines of text from serial port, save them to a file


import serial, io
import time
import os

s = open('log.dat', 'w')
log = time.strftime("%Y%m%d-%H%M%S")
s = open(log + '.dat', 'w')

delete = 'cat /dev/null > log.dat'

addr = '/dev/ttyACM0' # serial port to read data from
baud = 9600 # baud rate for serial port
fname = 'log.dat' # log file to save data in
fmode = 'a' # log file mode = append

with serial.Serial(addr,9600) as pt, open(fname,fmode) as outf:
spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
encoding='ascii', errors='ignore', newline='\r',line_buffering=$
spb.readline()
while (1):
now = time.time()
with open(fname,fmode) as outf:
while (time.time() - now) < 60:
x = spb.readline() # read one line of text from serial$
print (x,end='') # echo line of text on-screen
outf.write(x) # write line of text to file
outf.flush() # make sure it actually gets written
os.rename("/home/debian/fname", "/home/debian/log-
{}.dat".format(time.strftime("%y%m%d%H%M%S")))

if __name__ == '__main__':

main()

我不明白我在这里做错了什么。非常感谢任何建议或帮助。

最佳答案

尝试:

os.rename("/home/debian/"+fname, "/home/debian/log-           
{}.dat".format(time.strftime("%y%m%d%H%M%S")))

或:

os.rename("/home/debian/log.dat", "/home/debian/log-                             
{}.dat".format(time.strftime("%y%m%d%H%M%S")))

关于python - os.rename()错误不存在这样的文件或文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27963550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com