Copying a ZipExtFile

Moore, Mathew L MooreML at BATTELLE.ORG
Fri Oct 23 19:15:33 CEST 2009

Hello all,

A newbie here.  I was wondering why the following fails on Python 2.6.2 (r262:71605) on win32.  Am I doing something inappropriate?

Interestingly, it works in 3.1, but would like to also get it working in 2.6.

Thanks in advance,

import io
import shutil
import tempfile
import zipfile

with tempfile.TemporaryFile() as f:
    # (Real code retrieves archive via urllib2.urlopen().)
    zip = zipfile.ZipFile(f, mode='w')
    zip.writestr('unknowndir/src.txt', 'Hello, world!')

    # (Pretend we just downloaded the zip file.)

    # Result of urlopen() is not seekable, but ZipFile requires a
    # seekable file.  Work around this by copying the file into a
    # memory stream.
    with io.BytesIO() as memio:
        shutil.copyfileobj(f, memio)
        zip = zipfile.ZipFile(file=memio)
        # Can't use zip.extract(), because I want to ignore paths
        # within archive.
        src ='unknowndir/src.txt')
        with open('dst.txt', mode='wb') as dst:
            shutil.copyfileobj(src, dst)

The last line throws an Error:

Traceback (most recent call last):
  File "", line 25, in <module>
    shutil.copyfileobj(src, dst)
  File "C:\Python26\lib\", line 27, in copyfileobj
    buf =
  File "C:\Python26\lib\", line 594, in read
    bytes =
TypeError: integer argument expected, got 'long'

More information about the Python-list mailing list