2017-04-26 10 views
0

私はPythonには新しく、ファイルタイプに応じて、混乱したフォルダをサブフォルダに分類するスクリプトを作成しようとしています。何らかの理由で、スクリプトを実行しようとしたときにshutil.moveとエラーが発生しています。ディレクトリ上でファイルを移動するときに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

FIXED。

私はパスを渡す必要があったファイル名を渡していました。例えば。

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) 
関連する問題