有人可以告诉我为什么我的文件分子不起作用吗?

发布于 2025-02-03 09:42:57 字数 3104 浏览 4 评论 0原文

这是我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2025-02-10 09:42:57

目前尚不清楚显示的代码如何导致该错误。

这种代码应为“表驱动”,从而导致较少的代码和更多的可扩展性。

这样的事情:

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)

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)
几度春秋 2025-02-10 09:42:57

使用\\在使用/的路径中更改\\,它通常应该起作用,如果不存在,则不存在指定的文件(尝试更改它们或添加创建的条件文件夹如果找不到)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文