Hi,

I am zipping a folder and an error occurred in below code which says :
==> AttributeError: 'builtin_function_or_method' object has no attribute 'tell'

zipfile.py:

def write(self, filename, arcname=None, compress_type=None):
...
zinfo.header_offset = self.fp.tell()    # Start of header bytes
...
...

FYI:I have tried using "from zipfile import *" instead of "import zipfile", but of no avail.

What is causing this problem?

Regards,
Zia


My code to zip a folder:

import zipfile  [B]## i've tried with "from zipfile import *"[/B]
import os

def main():
    zip = "help3.zip"
    directory = "C:\MYDIR"
    toZip(directory)


def toZip(directory):
    zippedHelp = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )

    zipfile.debug = 3
    list = os.listdir(directory)

    for entity in list:
        each = os.path.join(directory,entity)

        if os.path.isfile(each):
            print each
            print os.path.basename(each)
            zippedHelp.write(each, os.path.basename(each), zipfile.ZIP_DEFLATED)
        else:
            addFolderToZip(zippedHelp,entity)

    zippedHelp.close()

def addFolderToZip(zippedHelp,folder):

    for file in folder:
            if os.path.isfile(file):
                zippedHelp.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
            elif os.path.isdir(file):
                addFolderToZip(zippedHelp,file)

main()

Dani AI

Generated

Brief diagnosis: ZipFile was being given the builtin zip function instead of a filename or an open file. When a non-string is passed, zipfile.ZipFile treats it as a file-like object and assigns it to self.fp. Later a call to self.fp.tell() fails because a builtin function has no tell() method. This typically happens when the archive-name variable is not in scope (so Python falls back to the builtin name) or when a variable called zip shadows the builtin.

Fixes and a safer pattern (builds on 's suggestion): pass the archive path into the zipping routine and avoid using zip as an identifier. Use a context manager and os.walk to preserve folder structure and avoid manual recursion:

import os, zipfile

archive_path = r"C:\output\help3.zip"
root_dir = r"C:\MYDIR"

with zipfile.ZipFile(archive_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for fname in filenames:
            full = os.path.join(dirpath, fname)
            arc = os.path.relpath(full, start=root_dir)
            zf.write(full, arc)

Troubleshooting tips: before creating the ZipFile print repr(archive_path) and type(archive_path) to confirm it's a string; avoid from zipfile import *; and use raw strings or doubled backslashes for Windows paths. These steps should resolve the AttributeError reported by .

you're going to kick yourself if you see this but the app is right except for one part, see you pass the directory path which is awesome but what about the zip filename.

zippedHelp = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )

see you using there but it hasn't been declared anywhere in that definition.
so when you call the toZip function do it like this:

toZip(zip, directory)

and receive it like such:

def toZip(zip, directory):

that should do the trick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.