我是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'
答案 0 :(得分:0)
固定。
我正在传递一个文件名,我需要传递路径。例如
public class theModel
{
public bool ChangedAction { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult ChangedAction(theModel theModel)
{
return View();
}
[HttpPost]
public ActionResult Index57(theModel theModel)
{
return View();
}