python - how to close the file after recording? -


Please help correct the script.

import urlib import re-import import import import pprint import request BS4 def make_catalog (): try: os.mkdir (for 'pick') except FileExistsError: print ('FileExistsError') Except exception permissionsError: except for PermissionError os.chdir (except 'exception'): except exception ('PermissionError'): print (exception) def change_catalog (): Try Print ('PermissionError') Except exception: Print (exception ) DEF download_image (path, name): # Urlib.URLopener () Retrieve (prefix + path, name) img = urllib.request.urlopen (prefix + path) .read (try): f = open (name, "wb") if f: print ('oserr: print (' osrerr ') Exception: Impressions (Exceptions) Finally: f.close () startIndex = 5794 endIndex = 5800 prefix = "http://www.inpic.ru" rep_chars = [' \\ ',' / in the range I '' '', ':', '' ',' ',' ',' ',' ',' '', '|', '-', '']] to make_catalog () StartIndex, endIndex): req = requests.get (prefix + '/ image /' + (str) if i req.status_code == requests.codes.ok: #print (i, '\ t', req.status_code, ' \ T ', req, end =' \ n ') Soup = BS 4. Beautiful Soup (Reik Content) # Print (soup. The word (); name = soup.find ("td", {"class": "post_title content" [1] .contents #author = soup.find ("div", {"class": "date_author"}) content [ 1] .contents Print ('NAME:', name [0]) #print (Author [0]) # Name [0] = re.sub ('[\\\\ / ... *]? & Lt; & gt; Rep]; [0] = name [0]. Rale (four, '_') print ('new name:', name [0]; [4] Rep_chars in four for ';', '_', name [0] ]) MainImagePath = soup.find ("img", {"class": "image final"}) ["src"] mainImageExt = mainImagePath.split () [- 1] manyImages = soup.findAll ('.' "Img Print '(' MANYIMAGE: \ n ') print (' MAINUMAGE: ', mainImagePath) print (' Original Quote EXT: ',' Image '}) print (multiple image) if Lane (multiple images) & gt; 1: Try the print ('create list'): os.mkdir (name [0]) FileExistsError #except: Except #Print PermissionError ('FileExistsError'): Except print ('PermissionError') exception: Print (exception ) Os.chdir (name [0]) #download_image (mainImagePath, str (name [0]) + '_0.' + MainImageExt) I = 0 for the name in many images: #print (name ['src'], End = '--- --------- \ n') download_image (name ['src'], str (name ['src'])) i = i + 1 os.chdir ('.. Print '(' image ') download_image (mainImagePath, str (name [0])'. '+' MainImageExt) Print ('mainImagePath', mainImagePath) print ('name', str (name [0] ) + '.' + MainImageExt) Print ('======================================= ====

even That's when the recordin

  traceback (most recent call final): "C: \ VINT \ OPENSERVER \ OpenServer \ domains \" images from the file group  

The following error message displays: Localhost \ python \ parse_html \ 1 \ q.py ", line 98, & lt; module & gt; download_image (name ['src'], str (name ['src'])) file "C: \ winters \ openserver \ openserver \ domain \ local host \ dragon \ parse_html \ 1 \ q.py", line 46, in download_image

You have to close the local variable 'F' one file referenced before the assignment. Trying f was never assigned because the open () call has taken an exception.

Use the end handler, as a reference manager:
  def download_image (path, name): # urlib .URLopener () Retrieve (prefix + path, name) img = urllib.request. Urlopen (prefix + path). Try: Open (name, "wb") as f: print ('open !!!') f.write (img) print ('write !!!') excluding OSError: except print (exception) Print (indispensable): print (exception)  

with the here statement will ensure that the f is closed for you If it was successfully opened, whatever happens.


Comments