file compression

E

Elaine Jackson

I'd like to use python for file compression/decompression. I see from the
manuals that there's a number of ways to do that, so I was hoping somebody could
point me toward the lightest-weight solution. The files I'm working with are
python modules and LaTeX source files, and I need to make them into archives so
I can store them in my website. Any help will be much appreciated. Thank you.

Peace
 
S

Skip Montanaro

Elaine> The files I'm working with are python modules and LaTeX source
Elaine> files, and I need to make them into archives so I can store them
Elaine> in my website.

Check out the zipfile module and class:

% pydoc zipfile
...
class ZipFile
| Class with methods to open, read, write, close, list zip files.
|
| z = ZipFile(file, mode="r", compression=ZIP_STORED)
|
| file: Either the path to the file, or a file-like object.
| If it is a path, the file will be opened and closed by ZipFile.
| mode: The mode can be either read "r", write "w" or append "a".
| compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib)
...

Skip
 
D

djw

Elaine said:
I'd like to use python for file compression/decompression. I see from the
manuals that there's a number of ways to do that, so I was hoping somebody could
point me toward the lightest-weight solution. The files I'm working with are
python modules and LaTeX source files, and I need to make them into archives so
I can store them in my website. Any help will be much appreciated. Thank you.

Peace

http://docs.python.org/lib/tar-examples.html

looks to be a good place to start....

-Don
 
G

Gerrit Muller

Skip said:
Elaine> The files I'm working with are python modules and LaTeX source
Elaine> files, and I need to make them into archives so I can store them
Elaine> in my website.

Check out the zipfile module and class:

% pydoc zipfile
...
class ZipFile
| Class with methods to open, read, write, close, list zip files.
|
| z = ZipFile(file, mode="r", compression=ZIP_STORED)
|
| file: Either the path to the file, or a file-like object.
| If it is a path, the file will be opened and closed by ZipFile.
| mode: The mode can be either read "r", write "w" or append "a".
| compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib)
...

Skip
I use ZipFile exactly as described here to pack my latex and visio
source files adn publish them on internet at the Gaudi systems
architecting website:
<http://www.extra.research.philips.com/natlab/sysarch/>. This works
great: only a few lines of code are needed.

regards, Gerrit
 
E

Elaine Jackson

Maybe I'm just dense but I can't figure out how to UNzip a zip file with the
code from the zipfile module. Would you mind showing me what that would look
like?

|
| Elaine> The files I'm working with are python modules and LaTeX source
| Elaine> files, and I need to make them into archives so I can store them
| Elaine> in my website.
|
| Check out the zipfile module and class:
|
| % pydoc zipfile
| ...
| class ZipFile
| | Class with methods to open, read, write, close, list zip files.
| |
| | z = ZipFile(file, mode="r", compression=ZIP_STORED)
| |
| | file: Either the path to the file, or a file-like object.
| | If it is a path, the file will be opened and closed by ZipFile.
| | mode: The mode can be either read "r", write "w" or append "a".
| | compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires
zlib)
| ...
|
| Skip
|
 
P

Peter Maas

Elaine said:
Maybe I'm just dense but I can't figure out how to UNzip a zip file with the
code from the zipfile module. Would you mind showing me what that would look
like?

z = ZipFile(file, mode="r", compression=ZIP_STORED)
contents = z.read()
z.close()

uz = file("unzippedfile", "wb")
uz.write(contents)
uz.close()

Mit freundlichen Gruessen,

Peter Maas
 
E

Elaine Jackson

| z = ZipFile(file, mode="r", compression=ZIP_STORED)
| contents = z.read()
| z.close()
|
| uz = file("unzippedfile", "wb")
| uz.write(contents)
| uz.close()

Hi. Thanks for responding. Your example looks completely reasonable, but when I
try it I get the following error message:

TypeError: read() takes exactly 2 arguments (1 given)

Does this make any sense to you?
 
P

Peter Maas

Elaine said:
Hi. Thanks for responding. Your example looks completely reasonable, but when I
try it I get the following error message:

TypeError: read() takes exactly 2 arguments (1 given)

Does this make any sense to you?

Replace

contents = z.read()

by

contents = z.read(nameOfFileInArchive)

An archive is a collection of files.
Mit freundlichen Gruessen,

Peter Maas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top