Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.zip not recognised if name changed on download 1

Status
Not open for further replies.

abitslow

Technical User
Apr 25, 2003
44
I scripted a download and unzip for .zip files. This works with no problem if the name of the .zip remains unchanged. If I try and change the name of the .zip on download then the zipfile.is_zipfile() won't recognise the file as a .zip file [though it still unzips in WinRAR].

I change the name by passing the shutil.copyfileobj() a different fdst name.

The download code used is:
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'))

The unzip code used is:
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 funtion 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 welcome!
Thanks.
 
I think we need to see the code that calls both of these functions
I suspect you are not passing the new name to the unzip function but cannot be sure

your download code could also be streamlined by always writing to outbasefilename but reassigning it first to the Url filename if None




A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Thnaks for that IPGuru. To answer your question:

The code does successfully change the name of the .zip file as I am testing it using a short .py with a "destfilename" intentionally there for that purpose and the result is a file with "that_new_name.zip"

What happens is that it won't then unzip.
 
SOLVED: thanks again IPGuru - you got me looking in the right place. I was changing the name but the .py which called the unzipper wasn't passing on a new name (if one was entered) from the downloader, just looking for the original from the url.
 
please show the code where you download the fill & then call the unzip routine.

I suspect you are not providing the NEW file-name to the unzip function

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top