简体   繁体   中英

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

I'm writing a program using python for a scout hut, to store information on scouts and be able to add/remove scouts. The spec said it HAS to use file handling to store scouts.

Upon writing the code for the remove scouts section, i encountered an issue with os.rename() command. I honestly don't understand why. The sed dir doesnt actually exist, is this my issue? The src dir does and i want to rename this to a different name. For example, i want to rename "IDTemp.txt" to "IDs.txt", without "IDs.txt" actually existing as a file. Is this possible? Code below:

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()

Output:

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'

Fixed it by rewriting the section of code, and copied the IDs from temp to a new copy of the main ID file, leaving behind the ID to remove remID . I then deleted the temp file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM