There is a problem when renaming a file by using the rename() function of python, which prompts windows error: [error 2]. The initial code is as follows:
def renameFile(filename):
filePre = "D:\\FileDemo\\Python\\pt.py"
os.rename(filePre, filename)
print os.listdir(filePre)
if __name__ == '__main__':
fileNew = "D:\\FileDemo\\Python\\Test.py"
renameFile(fileNew)
Later, after repeated attempts, the problem was solved~
Before rename, you need to use the chdir() function to enter the path of the target file to tell the python compiler where the file to be renamed is, and then you can modify it;
Python is not a terrible terminator. In fact, she is very young. She can’t find the file herself. We need to tell her where to find the path in detail and patiently os.path.dirname () function
import os
from nt import chdir
def renameF(preName, newName):
chdir(os.path.dirname(preName))
os.rename(preName, newName)
if __name__ == '__main__':
filePre = "D:\FileDemo\Python\PT.py"
fileNew = "D:\FileDemo\Python\Test.txt"
renameF(filePre, fileNew)
The code is very simple. You can rename any file by modifying filepre and FileNew.
Read More:
- Solution windowserror: [error 193]% 1 is not a valid Win32 Application
- WindowsError: [Error 2] The system cannot find the file specified
- Python calls DLL and reports an error windowserror: [error 126]
- Windowserror: [error 183] error and in Python os.raname () detailed explanation
- os.remove (file) error windowserror: [error 32]
- Workaround: windowserror: [error 193]% 1 is not a valid Win32 Application
- Solution / error of. Net framework 4.0 installation failure HResult 0xc8000222 solution
- Jenkins reported an error modulenotfounderror: no module named filename solution
- The problem of strncpy and the solution of GCC compile time checking
- Idapython: the solution of importing “site” failed error
- Pychar runtime error r6034 solution
- Python oserror: [errno 22] invalid argument: solution
- The solution of Android package error
- Solution of visdom enabling problem
- error:could not open…jvm.cfg solution
- Solution of CONDA (PIP) bad interpreter
- Notepad + + has no plug-in manager solution
- The solution of unsuccessful rosdep update
- Brief introduction of Linux MMAP and solution of bus error
- Ora-28547 of Navicat solution