[英]os.rename() error no such file or folder exists
我一直在嘗試創建一個Python腳本,該腳本將我的數據記錄到一個名為'log.dat'的.dat文件中,每分鍾將log.dat重命名為其他名稱,然后開始將傳入的日志數據寫入一個新的空日志中。 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")))
您沒有在open
通話中指定絕對路徑; 我們不知道當前目錄是否真的是/home/debian
; 因此,將絕對路徑與os.rename
一起使用是不明智的。 寧可寫
os.rename(fname, "log-{}.dat".format(time.strftime("%y%m%d%H%M%S")))
使用與open
相同的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.