我今天正在學習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'
所以我對錯誤感到困惑,因為我不知道我做錯了什么。
這是我正在使用的檔案夾:

uj5u.com熱心網友回復:
嘗試這個:
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427704.html
上一篇:大O表示法和凱撒密碼
