How to avoid creation of zero byte files upon closing a python file in 'w' mode due to an error? -
I am using Python 3.4.1 64bit on Windows 7. It's a question in hand, AFX, but I have included information in the case. Here is the relevant part of my code:
def download_and_save (URL_list, file name, download_dir): # Process list for URL, file name in zip (URL_list, file name): # complete path Generate name FILE_PATH = Open the file = Download_dir + filename #. Open (FILE_PATH, 'Mau') with local_file as: # Write whatever url is on try: local_file.write (urllib.request.urlopen (url) .read ()) urlib.request.URLError as e Except: ifattat (e, 'cause'): print ("We failed to reach server ...") Print (E. Creston) Alief Hatter (E, 'code'): Print ("The server requested the complete .. ") print (" Error code: ", e.code)
When I run this part of the code, I went to a bunch of errors ( Getaddrinfo failed - t Because of Ipo). When I checked the directory where it was saved later, I hope that a group of files with the file name was found, but zero bytes in length has not been written because it is understandable.
I am trying to understand why the perception in this case is not to touch one and instead of doing so, make the file. More importantly, can I stop this touch behavior so that no files are closed? Is there a better way of doing this, which can completely bypass the problem?
open
function file creates empty files in case you mistake Try to create a file after receiving the response from the server: Try
: data = urllib.request.urlopen (URL) .read (open) (FILE_PATH, ' Mau ') with local_file as: local_file.write (data) except urllib.request.URLError E: # ... exception handling
Comments
Post a Comment