I get the error :[WinError 2] The system cannot find the file specified:

I get the error :[WinError 2] The system cannot find the file specified:

此代码将所有 pdf 文件移动到名为 pdf.It 的文件夹中移动第一个文件然后移动文件出现错误:[WinError 2] 系统找不到指定的文件:'C:\Users\farbod\Desktop\Print Form.pdf' -> 'C:/Users/farbod/Desktop/pdf/Print Form.pdf' 注意:我还使用了 shutil 而不是 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

我认为这就是您要查找的代码

如果有任何错误请告诉我

import os
# from pathlib import Path

os.chdir("C:/Users/farbod/Desktop")
files = os.listdir()
for file in files:
    if file.split('.')[-1] == "pdf":
        os.rename(file, f"/pdf/{file}")
        # Path(file).rename(f"/pdf/{file}")
        # I can't understand why you have to move a file using 2 same functioning lines
        # (or guess what, i don't know to use pathlib. os.rename is enough to move a file)

已修复并正常工作code.Thank幽灵行动先生

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)