This is my code in python for my file sorter and i dont know why it dont work
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)
Here is my error
Enter the path you want to sort: C:\Document
Traceback (most recent call last):
File "C:\Tools\python\Portable_Python-3.9.9 x64\App\Python\lib\shutil.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:\Users\CLEMENT.LAPORTE\PycharmProjects\file-filter\sorter.py", line 33, in <module>
moveFile(path)
File "C:\Users\CLEMENT.LAPORTE\PycharmProjects\file-filter\sorter.py", line 29, in moveFile
shutil.move(file, "C:\\Users\\CLEMENT.LAPORTE\\Other\\")
File "C:\Tools\python\Portable_Python-3.9.9 x64\App\Python\lib\shutil.py", line 835, in move
copy_function(src, real_dst)
File "C:\Tools\python\Portable_Python-3.9.9 x64\App\Python\lib\shutil.py", line 444, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Tools\python\Portable_Python-3.9.9 x64\App\Python\lib\shutil.py", line 264, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'chrome_100_percent.pak'
This is the entire error message. I dont know why it dont works but i did it with a folder with 1 picture inside of it and my program worked. But when y tried with a folder with different type of file inside of it it dont work. Can someone help me please ?
(ps: some text are in french sorry)
CodePudding user response:
It's unclear how the code shown can result in that error.
This kind of code should be "table driven" which results in less code and more extensibility.
Something like this:
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)
CodePudding user response:
Change the \\
in the paths with /
and it should normally work, if not, the specified files do not exist (try to either change them or add a condition that create a folder if t doesn't find it)