2014-11-22 4 views
0

프로그래밍 기술을 향상시키면서 시험 웹 사이트에서 과거 논문을 일괄 다운로드하려고합니다. 나는 많은 것을 연구했고 이것이 왜 작동하지 않는지 모릅니다.여기에 왜 FileNotFoundException이 발생합니까?

오류 :

Downloading C:\Users\Azmat\Desktop\Practise Exams\Biology\January 2012 Question Paper.pdf 
Traceback (most recent call last): 
    File "testDownloader.py", line 31, in <module> 
    with open(filename, 'w+') as code: 
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Azmat\\Desktop\\Practise Exams\\Biology\\January 2012 Question Paper.pdf' 

내 코드 :

import urllib.request, urllib.parse, urllib.error 

years = [12, 13] 
months = ["JAN", "JUN"] 
subjects = ["BL", "CH", "PH"] 
files = ["QP", "W-MS", "WRE"] 
sub = { 
    'BL': 'Biology', 
    'CH': 'Chemistry', 
    'PH': 'Physics', 
} 
mon = { 
    'JAN' : 'January', 
    'JUN' : 'June', 
} 
fil = { 
    'QP' : 'Question Paper', 
    'W-MS' : 'Mark scheme', 
    'WRE' : "Examiner's report" 
} 

for year in years: 
    for month in months: 
     for subject in subjects: 
      for file in files: 
       url =('http://filestore.aqa.org.uk/subjects/AQA-%s1HP-%s-%s%d.PDF' % (subject, file, month, year)) 
       filename = 'C:\\Users\\Azmat\\Desktop\\Practise Exams\\' + sub[subject] + '\\' + mon[month] + ' 20%d ' % year + fil[file] + '.pdf' 
       print("Downloading " + filename) 
       f = urllib.request.urlopen(url) 
       data = f.read() 
       with open(filename, 'w+') as code: 
        code.write(data) 

답변

0

디렉토리는 당신이 쓰기 위해 파일을 열기 전에 존재하는 파일을 작성합니다. 단일 디렉토리의 경우 os.mkdir을, 재귀 적으로 디렉토리를 작성할 경우 os.makedirs을 사용할 수 있습니다.

import os 
for subject in sub.values(): 
    os.makedirs('C:\\Users\\Azmat\\Desktop\\Practise Exams\\' + subject) 
+0

고맙습니다. 문제가 해결되었습니다. – Injustice

관련 문제