I was study the import os
today, I working with a rename code.
JavaScript
x
16
16
1
import os
2
3
PictureFolder = "G:VSCodePythonVSWorkPicture"
4
i = 1
5
def renameFile():
6
for filename in os.listdir(PictureFolder):
7
global i
8
name, ext = os.path.splitext(filename)
9
if ext == ".jpg":
10
os.rename(filename, str(f"{i:03}") + ".jpg")
11
i += 1
12
else:
13
print("Processing")
14
15
renameFile()
16
and it was error due to:
JavaScript
1
3
1
FileNotFoundError
2
[WinError 2] The system cannot find the file specified: 'B.jpg' -> '001.jpg'
3
So I was confused with the error cause I don’t know what am I doing wrong.
This is the Folder I was working with:
Advertisement
Answer
Try this:
JavaScript
1
17
17
1
import os
2
3
PictureFolder = r"G:VSCodePythonVSWorkPicture"
4
os.chdir(PictureFolder)
5
i = 1
6
def renameFile():
7
for filename in os.listdir(PictureFolder):
8
global i
9
name, ext = os.path.splitext(filename)
10
if ext == ".jpg":
11
os.rename(filename, str(f"{i:03}") + ".jpg")
12
i += 1
13
else:
14
print("Processing")
15
16
renameFile()
17
Edit:
JavaScript
1
2
1
os.chdir(path): Change the current working directory to path.
2