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
import os from pathlib import Path path ="C:/Users/farbod/Desktop" pdf_folder_path = "C:/Users/farbod/Desktop/pdf" files=[] os.chdir(path) files = os.listdir(path) for file in files: file_path= path + '/' + file file_name,file_ext= os.path.splitext(file_path) if file_ext==".pdf": os.rename(file_path,pdf_folder_path+'/'+file) Path(file_path,).rename(pdf_folder_path+'/'+file) else: continue
Advertisement
Answer
fixed and working code.Thank you Mr. Ghost Ops
import os pdf_folder_path = "/pdf/" os.chdir("C:/Users/farbod/Desktop") files = os.listdir() for file in files: file_path= f"{file}" file_name,file_ext= os.path.splitext(file_path) if file_ext==".pdf": #your if statement didnt work because i had a folder called pdf. os.rename(file_path,"C:/Users/farbod/Desktop/pdf"+"/"+file)