在一个文件夹中移动同名文件而不被覆盖?
我制作了一个用于组织文件的脚本,并且运行良好。 当它覆盖同名文件时,会发生我的问题。 所以我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于每个人都有同样的问题,我找到了解决方案。
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.