簡體   English   中英

os.rename(src,dir)找不到指定的文件?

[英]os.rename(src,dir) cannot find specified file?

我正在使用python編寫用於偵察兵小屋的程序,以將信息存儲在偵察兵中,並能夠添加/刪除偵察兵。 規范說它必須使用文件處理來存儲偵察員。

在為remove scouts部分編寫代碼后,我遇到了os.rename()命令的問題。 老實說,我不明白為什么。 sed目錄實際上不存在,這是我的問題嗎? src目錄不,我想將此重命名為其他名稱。 例如,我想將“ IDTemp.txt”重命名為“ IDs.txt”,而實際上沒有“ IDs.txt”作為文件存在。 這可能嗎? 代碼如下:

elif self._name == "rem":
            remID = str(scoutID.get())
            if remID != "":
                #store all the lines that are in the file in a temp file
                with open(fileName,"r") as f:
                    lines = f.readlines()
                    with open(tempFileName,"a") as ft:
                        for line in lines:
                            #splits the line by ',', then takes the last part. It then trims this to remove the '/n' prefix
                            sctID = str(line.split(",")[3])[:-1]
                            if sctID != remID: #if the ID we are looking to remove isn't
                                #the ID of the scout we are currently looking at, move it to the temp file
                                ft.write(line)
                            else:
                                #Remove the scout ID of the scout that is being removed from the ID file
                                with open(IDFileName,"r+") as fi:
                                    lines = fi.readlines()
                                    with open(IDTemp,"a") as ft:
                                        for line in lines:
                                            if line[:-1] != sctID:
                                                ft.write(line)
                        os.rename(IDTemp,IDFileName)

                #remove the main file, then rectrate a new one
                os.remove(fileName)
                file = open(fileName,"a")
                file.close()

                #copy all the lines back to the main file
                with open(tempFileName,"r") as tf:
                    lines = tf.readlines()
                    with open(fileName,"a") as f:
                        for line in lines:
                            f.write(line)
                #finally, delete and recreate the temp file
                os.remove(tempFileName)
                file = open(tempFileName,"a")
                file.close()
            #remove the window    
            master.destroy()

輸出:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\KRIS\Documents\Python Projects\Scouts\popupWindow.py", line     88, in _callback
    os.rename(IDTemp,IDFileName)
FileNotFoundError: [WinError 2] The system cannot find the file specified:     'C:\\Users\\KRIS\\Scouts\\Temp\\IDTemp.txt' ->     'C:\\Users\\KRIS\\Scouts\\Temp\\IDs.txt'

通過重寫代碼部分進行了修復,並將ID從temp復制到了主ID文件的新副本中,留下了ID以刪除remID 然后,我刪除了臨時文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM