473,386 Members | 1,720 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,386 software developers and data experts.

Directory structure inside a ZipFile object

Hello everyone,

I'm a newbie to Python, learning it and tried to write the script that
would read file, zip it, and put in the target directory. I used
ZipFile module (code below).

It works nice, except the zipfile created contains the directory path
of the source file -- which I do NOT want to recreate. E.g. when
I compress 'c:\\temp\\zz.txt', the resulting zipfile contains not just
'zz.txt' file, but also the whole directory, i.e. first directory
is 'c:', inside of it the directory 'temp' is located, and only then
the 'zz.txt' file is stored.

Changing current directory via os.chdir and then using the
filename itself (zz.txt) doesn't work, i.e. the source directory
structure is still re-created within the zipfile.

Code follows. TIA for reply,
def zf(sfpath, targetdir):
if (sys.platform[:3] == 'win'):
tgfpath=sfpath[2:]
else:
tgfpath=sfpath
zfdir=os.path.dirname(os.path.abspath(targetdir) + tgfpath)
zfname=zfdir + '\\' + os.path.basename(tgfpath) + '.zip'
if(not os.path.isdir(zfdir)):
os.makedirs(zfdir)
archive=ZipFile(zfname, 'w', ZIP_DEFLATED)
print 'zfname ' + zfname
archive.write(sfpath)
archive.close()
zf('c:\\temp\\zz.txt','c:\\tmp\\test1\\test2') zfname c:\tmp\test1\test2\temp\zz.txt.zip


--
"Cultural ecstasy may have billions of participants, but it hardly
has a single friend." -- Charles Freund
Jul 18 '05 #1
2 3439
Bulba! wrote:
tried to read file, zip it, and put in the target directory....

It works nice, except the zipfile created contains the directory path
of the source file -- which I do NOT want to recreate.

Look into the two-argument form of the write command:

import zipfile
archive = zipfile.ZipFile('box.zip', 'w', zipfile.ZIP_DEFLATED)
archive.write('source/somewhere/one.txt', 'name.txt')
archive.write('two.txt', 'another.txt')
archive.close()

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
On Thu, 09 Dec 2004 07:42:57 -0800, Scott David Daniels
<Sc***********@Acm.Org> wrote:
Look into the two-argument form of the write command:
Well, I should have guessed that, it works. Thanks!

import zipfile
archive = zipfile.ZipFile('box.zip', 'w', zipfile.ZIP_DEFLATED)
archive.write('source/somewhere/one.txt', 'name.txt')
archive.write('two.txt', 'another.txt')
archive.close()

--Scott David Daniels
Sc***********@Acm.Org


--
It's a man's life in a Python Programming Association.
Jul 18 '05 #3

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

Similar topics

0
by: irit hasid | last post by:
Hi, I've been trying to use JDK's ZipFile.java in order to extract a zipped file. the files inside my zip file have japanese names, and it throws an exception. there's a bug at SUN's database...
6
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
7
by: Max M | last post by:
I guess that the best approach is calling a shell tool with something like os.popen(). But I cannot seem to find any free tools. Winzip has a command line option, but for registered users only....
1
by: ralobao | last post by:
I have this code: try: file = zipfile.ZipFile(nome_arquivo) Gauge.start() #inicia o Gauge for element in file.namelist(): try: newFile = open(diretorio + element,"wb") except: newFile =...
1
by: iamlevis3 | last post by:
Does Python have any internal facility for creating recursive archives of a directory? I'd like to avoid reliance on extenal tools (winzip,tar,etc). Thanks!
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
3
bvdet
by: bvdet | last post by:
Following is an example that may provide a solution to you: """ Function makeArchive is a wrapper for the Python class zipfile.ZipFile 'fileList' is a list of file names - full path each name...
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...
10
by: Bertsche | last post by:
I'm relatively new to python, and am trying to zip a directory containing several levels of files and folders. I can use the walk function to name each file in my directory, and I can use zipfile to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.