在一个文件夹中移动同名文件而不被覆盖?

发布于 2025-01-10 07:20:34 字数 1908 浏览 1 评论 0原文

我制作了一个用于组织文件的脚本,并且运行良好。 当它覆盖同名文件时,会发生我的问题。 所以我在 while 循环内创建了一个变量 [i] 来添加移动文件是否存在。

from genericpath import isdir
import os
import shutil

# Set File Path ------------------------------
print(os.getcwd())
path = os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Set Dict for files Extensions. -------------
IMAGES = (".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png")

VIDEOS = (
    ".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", 
        ".vob", ".mng", ".qt", ".mpg", ".mpeg", ".3gp"
    )

ARCHIVES = (
    ".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", 
        ".7z", ".dmg", ".rar", ".xar", ".zip"
    )

AUDIO = (
    ".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p",       
        ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", 
           ".wma"
    )

DOCUMENTS = (
    ".pdf", ".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf",
         ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx",     
             ".docm", ".dox", ".rvg", ".rtf", ".rtfd", ".wpd",
                ".xls", ".xlsx", ".ppt", "pptx", ".csv", ".txt", 
                   ".in", ".out"
    )

 PROGRAMS = (".exe", ".msi")

# Set list of Files in current dir. ----------
dir_list = os.listdir(path)

# Moving Files. ------------------------------
print('\n>>>> Start Organizing Files. <<<<\n')
for file in dir_list:
    i = 1
    if file == 'Folder-Organizer.py':
        pass

# Move [IMGs]. --------------------------------
    if file.endswith(IMAGES):
        while os.path.isfile(f'{path}\IMGs\{file}'):
            i += 1
            duplicated = os.path.splitext(file)
            file = f'{duplicated[0]}{i}{duplicated[1]}'
        
        shutil.copy(file, 'IMGs')
        os.remove(file)

但每次循环运行时都会引发此错误。

in copyfile with open(src, 'rb') as fsrc:    
FileNotFoundError: [Errno 2] No such file or directory:

I made a script for organize files, and it working well.
my issue happen when it overwritten files with the same name.
so i made a variable [i] inside while loop to added if the moving file exists.

from genericpath import isdir
import os
import shutil

# Set File Path ------------------------------
print(os.getcwd())
path = os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Set Dict for files Extensions. -------------
IMAGES = (".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png")

VIDEOS = (
    ".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", 
        ".vob", ".mng", ".qt", ".mpg", ".mpeg", ".3gp"
    )

ARCHIVES = (
    ".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", 
        ".7z", ".dmg", ".rar", ".xar", ".zip"
    )

AUDIO = (
    ".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p",       
        ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", 
           ".wma"
    )

DOCUMENTS = (
    ".pdf", ".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf",
         ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx",     
             ".docm", ".dox", ".rvg", ".rtf", ".rtfd", ".wpd",
                ".xls", ".xlsx", ".ppt", "pptx", ".csv", ".txt", 
                   ".in", ".out"
    )

 PROGRAMS = (".exe", ".msi")

# Set list of Files in current dir. ----------
dir_list = os.listdir(path)

# Moving Files. ------------------------------
print('\n>>>> Start Organizing Files. <<<<\n')
for file in dir_list:
    i = 1
    if file == 'Folder-Organizer.py':
        pass

# Move [IMGs]. --------------------------------
    if file.endswith(IMAGES):
        while os.path.isfile(f'{path}\IMGs\{file}'):
            i += 1
            duplicated = os.path.splitext(file)
            file = f'{duplicated[0]}{i}{duplicated[1]}'
        
        shutil.copy(file, 'IMGs')
        os.remove(file)

But it raising This Error each time the loop running.

in copyfile with open(src, 'rb') as fsrc:    
FileNotFoundError: [Errno 2] No such file or directory:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

べ映画 2025-01-17 07:20:34

对于每个人都有同样的问题,我找到了解决方案。
Shutil.move 引发错误(找不到文件) bcz while 循环更改 python 环境中的文件名,但不在目录中。
因此,您需要先使用 os.rename 模块将文件更改为新名称,然后使用 Shutil.move 完成其余操作。

For every one have the same issue, I found the solution.
The shutil.move raise error (file not found) bcz the while loop change file name inside python environment but not in the directory.
So you need to use os.rename module to change the file to the new name first then the shutil.move do the rest.

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