There was a problem renaming the file using the Python rename () function, prompting Windowserror: [Error 2] errors, the original code is as follows:
def RenameFile (filename): Filepre = " d:\\filedemo\\python\\pt.py " Span style= "color: #000000;" > Os.rename (filepre, filename) print Os.listdir (filepre) if __name__ = = '
Later, after repeated attempts, problem solving ~
Rename before the ChDir () function to enter the path to the destination file, the path is obtained through the Os.path.dirname () function:
# -*-coding:utf-8-*-
Import OS
From NT Import chdir
def RENAMEF (Prename, newName): chdir (Os.path.dirname (prename)) os.rename (Prename, newName)if __name__'__main__': "D:\FileDemo\ python\pt.py ""D:\FileDemo\Python\Test.txt " RENAMEF (Filepre, FileNew)
The code is very concise and you can rename any file by modifying filepre,filenew.
Solution: Windowserror: [Error 2]