os.rename() 函数导致 FileNotFoundError
os.rename() function is causing a FileNotFoundError
我今天正在研究 import os
,我正在使用重命名代码。
import os
PictureFolder = "G:\VSCode\PythonVS\WorkPicture"
i = 1
def renameFile():
for filename in os.listdir(PictureFolder):
global i
name, ext = os.path.splitext(filename)
if ext == ".jpg":
os.rename(filename, str(f"{i:03}") + ".jpg")
i += 1
else:
print("Processing")
renameFile()
错误是由于:
FileNotFoundError
[WinError 2] The system cannot find the file specified: 'B.jpg' -> '001.jpg'
所以我对错误感到困惑,因为我不知道我做错了什么。
这是我正在使用的文件夹:
试试这个:
import os
PictureFolder = r"G:\VSCode\PythonVS\WorkPicture"
os.chdir(PictureFolder)
i = 1
def renameFile():
for filename in os.listdir(PictureFolder):
global i
name, ext = os.path.splitext(filename)
if ext == ".jpg":
os.rename(filename, str(f"{i:03}") + ".jpg")
i += 1
else:
print("Processing")
renameFile()
编辑:
os.chdir(path): Change the current working directory to path.
我今天正在研究 import os
,我正在使用重命名代码。
import os
PictureFolder = "G:\VSCode\PythonVS\WorkPicture"
i = 1
def renameFile():
for filename in os.listdir(PictureFolder):
global i
name, ext = os.path.splitext(filename)
if ext == ".jpg":
os.rename(filename, str(f"{i:03}") + ".jpg")
i += 1
else:
print("Processing")
renameFile()
错误是由于:
FileNotFoundError
[WinError 2] The system cannot find the file specified: 'B.jpg' -> '001.jpg'
所以我对错误感到困惑,因为我不知道我做错了什么。
这是我正在使用的文件夹:
试试这个:
import os
PictureFolder = r"G:\VSCode\PythonVS\WorkPicture"
os.chdir(PictureFolder)
i = 1
def renameFile():
for filename in os.listdir(PictureFolder):
global i
name, ext = os.path.splitext(filename)
if ext == ".jpg":
os.rename(filename, str(f"{i:03}") + ".jpg")
i += 1
else:
print("Processing")
renameFile()
编辑:
os.chdir(path): Change the current working directory to path.