谁能告诉我为什么我的文件分类不工作?



这是我的python代码为我的文件排序器,我不知道为什么它不工作

import os
import shutil
path = str(input("Enter the path you want to sort: "))
def moveFile(path):
path1 = path
path = os.listdir(path)
for file in path:
if file.endswith(".gif") or file.endswith(".jfif") or file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\Pictures\Pictures\")
break
elif file.endswith(".mp4") or file.endswith(".mkv") or file.endswith(".avi"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\Videos\Videos\")
break
elif file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".m4a"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\Music\Songs\")
break
elif file.endswith(".exe"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\App\")
break
elif file.endswith(".txt") or file.endswith(".docx") or file.endswith(".pptx") or file.endswith(".pdf"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\Work\")
break
elif file.endswith(".py") or file.endswith(".c") or file.endswith(".cpp") or file.endswith(".java") or file.endswith(".js") or  file.endswith(".html") or file.endswith(".css"):
shutil.move(f"{path1}\{file}", "C:\Users\CLEMENT.LAPORTE\Code\")
break
else:
shutil.move(file, "C:\Users\CLEMENT.LAPORTE\Other\")
break
print(f"Moved:t{file}t")
moveFile(path)

这是我的错误

Enter the path you want to sort: C:Document
Traceback (most recent call last):
File "C:ToolspythonPortable_Python-3.9.9 x64AppPythonlibshutil.py", line 815, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable: 'chrome_100_percent.pak' -> 'C:\Users\CLEMENT.LAPORTE\Other\chrome_100_percent.pak'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersCLEMENT.LAPORTEPycharmProjectsfile-filtersorter.py", line 33, in <module>
moveFile(path)
File "C:UsersCLEMENT.LAPORTEPycharmProjectsfile-filtersorter.py", line 29, in moveFile
shutil.move(file, "C:\Users\CLEMENT.LAPORTE\Other\")
File "C:ToolspythonPortable_Python-3.9.9 x64AppPythonlibshutil.py", line 835, in move
copy_function(src, real_dst)
File "C:ToolspythonPortable_Python-3.9.9 x64AppPythonlibshutil.py", line 444, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:ToolspythonPortable_Python-3.9.9 x64AppPythonlibshutil.py", line 264, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'chrome_100_percent.pak'

这是整个错误消息。我不知道为什么它不工作,但我做了一个文件夹里面有1张图片,我的程序工作。但是当我尝试用不同类型的文件在它里面的文件夹,它不工作。有人能帮我一下吗?

(ps:抱歉,有些文字是法语)

目前还不清楚显示的代码是如何导致这个错误的。

这类代码应该是"表驱动"的。这使得代码更少,可扩展性更强。

像这样:

from glob import glob
from os.path import join, isfile
from shutil import move
from pathlib import Path
BASE = 'C:\Users\CLEMENT.LAPORTE'
DEFAULT = 'Other'
CONTROL = {('gif', 'jfif', 'jpg', 'jpeg', 'png'): 'Pictures\Pictures',
('mp4', 'mkv', 'avi'): 'Videos\Videos',
('mp3', 'wav', 'm4a'): 'Music\Songs',
('exe',): 'App',
('txt', 'docx', 'pptx', 'pdf'): 'Work',
('py', 'c', 'cpp', 'java', 'js', 'html', 'css'): 'Code'}

def moveFiles(path):
for file in glob(join(path, '*.*'):
suffix = Path(file).suffix[1:]
for k, v in CONTROL.items():
if suffix in k:
p = v
break
else:
p = DEFAULT
try:
move(file, join(BASE, p))
print(f"Moved: {file} -> {p}")
except Exception as e:
print(f'Failed to move {file} -> {p} due to {e}')

path = input("Enter the path you want to sort: ")
moveFiles(path)

/更改路径中的\,它应该正常工作,如果没有,指定的文件不存在(尝试更改它们或添加一个条件,如果t找不到则创建一个文件夹)

最新更新