this code moves all pdf files into a folder called pdf.It moves the first file then get error for moved file: [WinError 2] The system cannot find the file specified: ‘C:UsersfarbodDesktopPrint Form.pdf’ -> ‘C:/Users/farbod/Desktop/pdf/Print Form.pdf’ note: I also used shutil instead of pathlib.Same error
JavaScript
x
23
23
1
2
import os
3
from pathlib import Path
4
5
6
7
8
path ="C:/Users/farbod/Desktop"
9
pdf_folder_path = "C:/Users/farbod/Desktop/pdf"
10
files=[]
11
12
os.chdir(path)
13
files = os.listdir(path)
14
for file in files:
15
file_path= path + '/' + file
16
file_name,file_ext= os.path.splitext(file_path)
17
if file_ext==".pdf":
18
os.rename(file_path,pdf_folder_path+'/'+file)
19
Path(file_path,).rename(pdf_folder_path+'/'+file)
20
else:
21
continue
22
23
Advertisement
Answer
fixed and working code.Thank you Mr. Ghost Ops
JavaScript
1
19
19
1
import os
2
3
4
pdf_folder_path = "/pdf/"
5
6
os.chdir("C:/Users/farbod/Desktop")
7
files = os.listdir()
8
for file in files:
9
file_path= f"{file}"
10
file_name,file_ext= os.path.splitext(file_path)
11
if file_ext==".pdf":
12
#your if statement didnt work because i had a folder called pdf.
13
os.rename(file_path,"C:/Users/farbod/Desktop/pdf"+"/"+file)
14
15
16
17
18
19