[英]Error with shutil.move when moving files over directories
我是Python的新手,正在嘗試編寫一個腳本,該腳本將根據文件類型將混亂的文件夾分類為子文件夾。 由於某些原因,嘗試運行腳本時, shutil.move出現錯誤。
請原諒效率低下的代碼-我已經進行了大量更改以嘗試調試! 請找到隨附的我的代碼和錯誤消息。
import os
import shutil
filename = os.getcwd()
print filename
print filename + '/Images'
#sort IMAGES
if os.path.exists(filename + '/Images'):
print "Yes, Image folder exists"
else:
print "No, it does not exists "
print "Creating new directory for Images"
os.makedirs(filename + '/Images')
filename1 = filename + '/Images'
#os.chdir(filename1)
for file in os.listdir(filename):
if file.endswith(".JPG"):
shutil.move(file,filename1)
elif file.endswith(".png"):
shutil.move(file,filename1)
elif file.endswith(".gif"):
shutil.move(file,filename1)
#sort Videos
if os.path.exists(filename + '/Videos'):
print "Yes, Videos folder exists"
else:
print "No, it does not exists "
print "Creating new directory for Videos"
os.makedirs(filename + '/Videos')
filename1 = filename + '/Videos'
#os.chdir(filename1)
for file in os.listdir(filename):
if file.endswith(".mov"):
shutil.move(file,filename1)
elif file.endswith(".avi"):
shutil.move(file,filename1)
elif file.endswith(".mkv"):
shutil.move(file,filename1)
#sort SONGS
if os.path.exists(filename + '/Music'):
print "Yes, Music folder exists"
else:
print "No, it doesnot exists "
print "Creating new directory for Music"
os.makedirs(filename + '/Music')
filename1 = filename + '/Music'
#os.chdir(filename1)
for file in os.listdir(filename):
if file.endswith(".mp3"):
shutil.move(file,filename1)
elif file.endswith(".wav"):
shutil.move(file,filename1)
elif file.endswith(".wma"):
shutil.move(file,filename1)
#sort DOCUMENTS
if os.path.exists(filename + '/Documents'):
print "Yes, Documents folder exists"
else:
print "No, it doesnot exists "
print "Creating new directory for Documents"
os.makedirs(filename + '/Documents')
filename1 = filename + '/Documents'
#os.chdir(filename1)
#check if it is neccessary to create new sub-directories for document types
contains_pdf = False
contains_doc = False
contains_ppt = False
for file in os.listdir(filename1):
if file.endswith(".pdf"):
contains_pdf = True
if file.endswith(".docx"):
contains_doc = True
if file.endswith(".txt"):
contains_doc = True
if file.endswith(".ppt"):
contains_ppt = True
#create subdirectories if does not exist
if os.path.exists(filename + '/Documents/PDF') == False and contains_pdf == True:
os.makedirs(filename1 + '/PDF')
elif os.path.exists(filename + '/Documents/DOC') == False and contains_doc == True:
os.makedirs(filename1 + '/DOC')
elif os.path.exists(filename + '/Documents/PPT') == False and contains_ppt == True:
os.makedirs(filename1 + '/PPT')
filename2 = filename1
#move subtypes of documents in correct folders - THIS IS WHERE I GET MY ERROR!
for file in os.listdir(filename1):
if file.endswith(".pdf"):
filename2 = filename1 + '/PDF'
shutil.move(file, filename2)
if file.endswith(".docx"):
shutil.move(file,filename1 + '/DOC')
if file.endswith(".txt"):
shutil.move(file,filename1 + '/DOC')
if file.endswith(".ppt"):
shutil.move(file,filename1 + '/PPT')
filename2 = filename1
我不知道。 這是我得到的錯誤。
Traceback (most recent call last):
File "organise.py", line 104, in <module>
shutil.move(file,filename2)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '03_Anesthetic considerations in severe ankylosing.pdf'
固定。
我在傳遞文件名時需要傳遞路徑。 例如。
for file in os.listdir(filename):
if file.endswith(".pdf"):
shutil.move(filename + file, filename1 + '/PDF/' + file)
if file.endswith(".PDF"):
shutil.move(filename + file, filename1 + '/PDF/' + file)
if file.endswith(".docx"):
shutil.move(filename + file, filename1 + '/DOC/' + file)
if file.endswith(".txt"):
shutil.move(filename + file, filename1 + '/DOC/' + file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.