Python 3.3: .zip not recognized if name changed at boot

I wrote a script to download and unzip .zip files using Python 3.3. This works without problems if the .zip name remains unchanged. If I try to change the name of the .zip at boot time, then zipfile.is_zipfile () will not recognize the file as a .zip file [although it is still unpacked in WinRAR].

I change the name by passing shutil.copyfileobj () another name fdst (not the whole path).

Used download code:

import urllib.request import shutil import os, os.path def mjd_downloader(url, destdir, destfilename=None): req = urllib.request.Request(url) response = urllib.request.urlopen(req) #if no filename passed by destfilename, retrieve filename from ulr if destfilename is None: #need to isolate the file name from the url & download to it filename = os.path.split(url)[1] else: #use given filename filename = destfilename #'Download': write the content of the downloaded file to the new file shutil.copyfileobj(response, open(os.path.join(destdir,filename), 'wb')) 

Used unzip code:

 import zipfile from zipfile import ZipFile import os, os.path import shutil def mjd_unzipper(zippathname, outfilebasename=None): #outfilebasename is a name passed to the function if a new name for #the content is requried if zipfile.is_zipfile(zippathname) is True: zfileinst = ZipFile(zippathname, 'r') zfilepath = os.path.split(zippathname)[0] zlen = len(zfileinst.namelist()) print("File path: ", zfilepath) if outfilebasename is not None: for filename in zfileinst.namelist(): memtype = os.path.splitext(filename)[1] outfilename = os.path.join(outfilebasename + memtype) print("Extracting: ", filename, " - to: ", outfilename) #curzfile = zfileinst.read(filename) curzfile = zfileinst.open(filename) shutil.copyfileobj(curzfile, open( os.path.join(zfilepath, outfilename), 'wb')) else: for i in range(zlen): extractfile = zfileinst.namelist()[i] memtype = os.path.splitext(extractfile)[1] zfileinst.extract(extractfile, path = zfilepath) zipfile.ZipFile.close(zfileinst) else: print("Is not a zipfile") pass 

Any thoughts are welcome.

+4
source share
1 answer

I think your file has never been closed: on line 24 of your script load, you opened the destination file to write binary data. Only when you call close() in the same file that the data is pushed from memory to the file (there is also a method for calling without closing the file, but I can’t remember it right now).

In general, it is best to open file objects using the with statement:

 with open(os.path.join(destdir,filename), 'wb') as f: shutil.copyfileobj(response, f) 

When the with statement is used with file objects, it automatically closes them at the end of the block, if you ever break out of the block, or if the block is left for any other reason (there may be an unhandled exception due to which the interpreter should exit).

If you cannot use the with statement (I think some older versions of Python do not support its use with file objects), you can simply call close () on the object after you finish:

 f = open(os.path.join(destdir,filename), 'wb') shutil.copyfileobj(response, f) f.close() 

I hope this is the reason, because then it will be a simple fix!

0
source

Source: https://habr.com/ru/post/1446267/


All Articles