Brad
Brad

Reputation: 87

after unziping not able to write the file from source to destination in python

I am using the following code to unzip. It's able to read and print the source. However it is not writing to the destination

my_dir = r"D:\Download"
my_zip = r"D:\Download\my_file.zip"
with zipfile.ZipFile(my_zip) as zip_file:
    for member in zip_file.namelist():
        filename = os.path.basename(member)
        # skip directories
        if not filename:
            continue
        #copy file (taken from zipfile's extract)
        source = zip_file.open(member)
        target = file(os.path.join(my_dir, filename), "wb")
        with source, target:
            shutil.copyfileobj(source, target)

Further information that the author left in the comment:

Yes, I type the script name in a console. Though it is able to read the file, its not able to write it to the destination. It throws an error "str object not callable" – Brad 4 hours ago

The problem is with this line: target = file(os.path.join(my_dir, filename), "wb") TypeError: 'str' object is not callable – Brad 4 hours ago

Upvotes: 1

Views: 100

Answers (1)

starrify
starrify

Reputation: 14771

The problem is with this line: target = file(os.path.join(my_dir, filename), "wb") TypeError: 'str' object is not callable

It's highly suspected that you have assigned a string to the name file previously, e.g. file = 'foo.py'. Please check your code.

Also, instead of file(os.path.join(my_dir, filename), "wb"), you could also use open(os.path.join(my_dir, filename), "wb")

The next time please include all related information (e.g. details of the error) in the question body.

Upvotes: 1

Related Questions