Python 在目录上移动文件时,shutil.move出错
我是Python新手,正在尝试编写一个脚本,根据文件类型将杂乱的文件夹分类到子文件夹中。由于某些原因,我在尝试运行脚本时,shutil.move出现错误 请原谅我的代码效率低下-我已经改变了很多尝试和调试!请查看附件中的我的代码和错误消息Python 在目录上移动文件时,shutil.move出错,python,shutil,Python,Shutil,我是Python新手,正在尝试编写一个脚本,根据文件类型将杂乱的文件夹分类到子文件夹中。由于某些原因,我在尝试运行脚本时,shutil.move出现错误 请原谅我的代码效率低下-我已经改变了很多尝试和调试!请查看附件中的我的代码和错误消息 import os import shutil filename = os.getcwd() print filename print filename + '/Images' #sort IMAGES if os.path.exists(filen
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'
回溯(最近一次呼叫最后一次):
文件“Organize.py”,第104行,在
move(文件,文件名2)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”,第302行,移动中
副本2(src、real_dst)
copy2中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”,第130行
复制文件(src、dst)
copyfile中的第82行文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”
开放式(src,'rb')作为fsrc:
IOError:[Errno 2]没有这样的文件或目录:“03\u严重强直中的麻醉注意事项.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)