473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 2103
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()
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.