有人可以告诉我为什么我的文件分子不起作用吗?
这是我在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:\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'
,这是整个错误消息。我不知道为什么它不起作用,但是我用一个带有1张图片的文件夹来完成,并且我的程序奏效了。但是,当Y尝试使用其中包含不同类型的文件的文件夹时,它不起作用。 有人可以帮我吗?
(PS:有些文字在法语中,对不起)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚显示的代码如何导致该错误。
这种代码应为“表驱动”,从而导致较少的代码和更多的可扩展性。
这样的事情:
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:
使用
\\
在使用/
的路径中更改\\
,它通常应该起作用,如果不存在,则不存在指定的文件(尝试更改它们或添加创建的条件文件夹如果找不到)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)