472,796 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

Zipping files/zipfile module

This will probably sound like a very dumb question.

I am trying to zip some files within a directory.

I want to zip all the files within a directory called "temp"
and have the zip archive saved in a directory with temp called ziptemp

I was trying to read up on how to use the zipfile module python
provides, but I cannot seem to find adequate documentation on function
itself.

Perhaps someone could help me in this task?

I am guessing it must be something like the shutile module
something like copy(src,dst)

THank you

Stephen

Aug 2 '06 #1
5 2066
OriginalBrownster wrote:
I want to zip all the files within a directory called "temp"
and have the zip archive saved in a directory with temp called ziptemp

I was trying to read up on how to use the zipfile module python
provides, but I cannot seem to find adequate documentation on function
itself.

Perhaps someone could help me in this task?
Hello,

This isn't completely tested, but perhaps it will help you get started:

from os import listdir, mkdir
from os.path import join, basename, isfile
from zipfile import ZipFile

def zip_dir(path, output_path, include_hidden=True):
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
try:
mkdir(output_path)
except OSError, e:
if e.errno == 17: # Path exists
pass
zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
for f in files:
if basename(f).startswith('.') and not include_hidden:
continue
print "Adding %s to archive..." % (f,)
zip_file.write(f)
zip_file.close()

Use like:
zip_dir('temp', 'temp/ziptemp')

Note that if you want to add the entire contents of a directory
(subdirectories, recursively), you should consider using os.walk or
something similar. This will only add the file contents of the directory.
I'm not sure if the zipfile module provides any nice ways to write
directories to the archive, but I'm assuming it just involves writing an
arcname with a '/' in it (see help(zipfile.ZipFile)).

--
Brian Beck
Adventurer of the First Order
Aug 2 '06 #2
Brian Beck wrote:
OriginalBrownster wrote:
I want to zip all the files within a directory called "temp"
and have the zip archive saved in a directory with temp called ziptemp

I was trying to read up on how to use the zipfile module python
provides, but I cannot seem to find adequate documentation on function
itself.

Perhaps someone could help me in this task?

Hello,

This isn't completely tested, but perhaps it will help you get started:

from os import listdir, mkdir
from os.path import join, basename, isfile
from zipfile import ZipFile

def zip_dir(path, output_path, include_hidden=True):
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
try:
mkdir(output_path)
except OSError, e:
if e.errno == 17: # Path exists
pass
zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
for f in files:
if basename(f).startswith('.') and not include_hidden:
continue
print "Adding %s to archive..." % (f,)
zip_file.write(f)
zip_file.close()

Use like:
zip_dir('temp', 'temp/ziptemp')

Note that if you want to add the entire contents of a directory
(subdirectories, recursively), you should consider using os.walk or
something similar. This will only add the file contents of the directory.
I'm not sure if the zipfile module provides any nice ways to write
directories to the archive, but I'm assuming it just involves writing an
arcname with a '/' in it (see help(zipfile.ZipFile)).

--
Brian Beck
Adventurer of the First Order
To avoid calling os.path.join() twice for each filename when you build
the list of files you could write the list comprehension like so:

[n for n in (join(path, f) for f in listdir(path)) if isfile(n)]

Also, you should use the "symbolic" errors from the errno module rather
than hard-coding a constant:

from errno import EEXIST
....
if e.errno == EEXIST: # Path exists

Finally, if your using a single arg with a string interpolation and you
know it'll never be a tuple you needn't wrap it in a tuple:

print "Adding %s to archive..." % f

Aug 2 '06 #3
Simon Forman a écrit :
Brian Beck wrote:
>OriginalBrownster wrote:
>>I want to zip all the files within a directory called "temp"
and have the zip archive saved in a directory with temp called ziptemp

I was trying to read up on how to use the zipfile module python
provides, but I cannot seem to find adequate documentation on function
itself.

Perhaps someone could help me in this task?
Hello,

This isn't completely tested, but perhaps it will help you get started:

from os import listdir, mkdir
from os.path import join, basename, isfile
from zipfile import ZipFile

def zip_dir(path, output_path, include_hidden=True):
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
try:
mkdir(output_path)
except OSError, e:
if e.errno == 17: # Path exists
pass
zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
for f in files:
if basename(f).startswith('.') and not include_hidden:
continue
print "Adding %s to archive..." % (f,)
zip_file.write(f)
zip_file.close()

Use like:
zip_dir('temp', 'temp/ziptemp')

Note that if you want to add the entire contents of a directory
(subdirectories, recursively), you should consider using os.walk or
something similar. This will only add the file contents of the directory.
I'm not sure if the zipfile module provides any nice ways to write
directories to the archive, but I'm assuming it just involves writing an
arcname with a '/' in it (see help(zipfile.ZipFile)).

--
Brian Beck
Adventurer of the First Order

To avoid calling os.path.join() twice for each filename when you build
the list of files you could write the list comprehension like so:

[n for n in (join(path, f) for f in listdir(path)) if isfile(n)]

Also, you should use the "symbolic" errors from the errno module rather
than hard-coding a constant:

from errno import EEXIST
...
if e.errno == EEXIST: # Path exists

Finally, if your using a single arg with a string interpolation and you
know it'll never be a tuple you needn't wrap it in a tuple:

print "Adding %s to archive..." % f
Other solutions:
you can try the rar command line from WinRar but it's not recommended.
This is a very slow manner to compress file. Or you can try the Bz
module of python.
Aug 2 '06 #4
Ant
Enabling directory recursion:
from os import listdir, mkdir
from os.path import join, basename, isfile
from zipfile import ZipFile

def zip_dir(path, output_path, include_hidden=True):
try:
mkdir(output_path)
except OSError, e:
if e.errno == 17: # Path exists
pass
zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
for root, dirs, files in os.walk(dir):
for f in files:
fp = path.join(root, f)
zip_file.write(fp, fp[len(dir):]) # Write to zip as a
path relative to original dir.
zip_file.close()
Aug 2 '06 #5
Yves Lange wrote:
Other solutions:
you can try the rar command line from WinRar but it's not recommended.
This is a very slow manner to compress file.
Are you sure? This worked about 4 times faster than the zip command line
utility in Linux, compressing the same files...

--
Brian Beck
Adventurer of the First Order
Aug 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug Tolton | last post by:
Is there a simple way to zip and unzip files? I'm looking for something along the lines of: zfile = zipfile(r'c:\somefile.zip') zfile.extract(r'c:\somefiles') I've looked at the...
8
by: Oriana | last post by:
Hi! I'm beginning to use the zipfile module in Python and I'm confused about something. I am trying to extract certain files from one zip and copy them into another one. This is the code I‘ve...
5
by: Lorn | last post by:
Is there a limitation with python's zipfile utility that limits the size of a file that can be extracted? I'm currently trying to extract 125MB zip files with files that are uncompressed to > 1GB...
4
by: OriginalBrownster | last post by:
Hi There. I'm very new to python, and I have been using the TurboGears Framework to use python to power my application. I have a class which handles the upload of a file to a directory called...
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
8
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B | last post by:
I made a C/S network program, the client receive the zip file from the server, and read the data into a variable. how could I process the zipfile directly without saving it into file. In the...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
3
by: dp_pearce | last post by:
Hi all, I have come across an error while using zipfile and I can't seem to find somewhere that explains the problem. My script needs to be able to take text files from one drive and add them to...
1
by: Bouzy | last post by:
I wrote this script... #!/usr/bin/python # Filename: backup_zip.py import os, zipfile, time, datetime, glob from os.path import splitext, relpath, split r = 1 cwd = os.getcwd()
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.