问题描述
我正在尝试在我的下载文件夹中找到所有下载的mp4和mkv文件。我要查找的特定文件都位于下载的不同位置,有些位于下载文件夹的子目录中,有些只是下载文件夹中的文件。
这是我到目前为止所拥有的
import os
import shutil
os.chdir(r'C:\Users\carte\Downloads')
path = os.path.abspath(r'.\Movie_files')
for p,d,f in os.walk(r'C:\Users\carte\Downloads'):
for file in f:
if file.endswith('.mp4') or file.endswith('.mkv'):
print('-------------------------------------------------------')
print('File Path:' + os.path.abspath(file))
print(f"Movie File:{file}")
print('-------------------------------------------------------')
movie_file_path =os.path.abspath(file)
shutil.move(movie_file_path,path)
但是我在跑步时仍然会收到此错误
-------------------------------------------------------
File Path:C:\Users\carte\Downloads\1917.mp4
Movie File:1917.mp4
-------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\carte\AppData\Local\Programs\Python\python38-32\lib\shutil.py",line 788,in move
os.rename(src,real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\carte\\Downloads\\1917.mp4' -> 'C:\\Users\\carte\\Downloads\\Movie_files\\1917.mp4'
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "c:/Users/carte/OneDrive/Documents/Code/Practice/practice_os.py",line 13,in <module>
shutil.move(movie_file_path,path)
File "C:\Users\carte\AppData\Local\Programs\Python\python38-32\lib\shutil.py",line 802,in move
copy_function(src,real_dst)
File "C:\Users\carte\AppData\Local\Programs\Python\python38-32\lib\shutil.py",line 432,in copy2
copyfile(src,dst,follow_symlinks=follow_symlinks)
File "C:\Users\carte\AppData\Local\Programs\Python\python38-32\lib\shutil.py",line 261,in copyfile
with open(src,'rb') as fsrc,open(dst,'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\carte\\Downloads\\1917.mp4'
PS C:\Users\carte\OneDrive\Documents\Code>
shutil.move为什么要查找试图在目标位置移动的文件?
解决方法
在组织代码时,我遇到了同样的问题,这与我需要使用\
的字符串中的\\
有关。
我将在下面粘贴我编写的代码。 它可以正常工作,因此您可以尝试根据需要进行更新。
from shutil import move as mve
from time import sleep as slp
import os
os.chdir('c:\\Users\\Charlie\\Downloads')
source_path = "c:\\Users\\Charlie\\Downloads"
dest_path = "c:\\users\\Charlie\\Downloads\\FORGOT TO USE A PATH"
def move_file():
# get the current date
src_path = os.path.join(source_path,f)
# create the folders if they arent already exists
if not os.path.exists(dest_path):
os.makedirs(dest_path)
if not os.path.exists(f"{dest_path}\\{f}"):
mve(src_path,dest_path)
else:
print("File already exists")
while True:
files = os.listdir("c:\\Users\\Charlie\\Downloads")
for f in files:
if f.endswith('.zip'):
dest_path = "c:\\users\\Charlie\\Downloads\\Zip"
move_file()
elif f.endswith('.pdf') or f.endswith('.docx') or f.endswith('.doc') or f.endswith('.ppt') or f.endswith('.pptx'):
dest_path = "c:\\users\\Charlie\\Downloads\\Documents"
move_file()
elif f.endswith('.jpg') or f.endswith('.png'):
dest_path = "c:\\users\\Charlie\\Downloads\\Pictures"
move_file()
elif f.endswith('.tmp'):
break
elif not os.path.isdir(f):
dest_path = "c:\\users\\Charlie\\Downloads\\Unsorted"
move_file()
slp(10)
,
检查目标目录是否存在。
此外,目的地实际上是源代码树的一部分也可能是一个问题,因此您可能希望将文件放在其他位置。