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

can tarfile maintain directory structure?

Is there a way to use the tarfile module to recursively compress the
contents of a directory and maintain the directory structure in the
tar archive?

Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
directory)
works great on linux, but I need this script to work on windows as
well :(
Jul 18 '05 #1
8 11033
[Jay Donnell]
Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
directory) works great on linux, but I need this script to work on
windows as well :(


GNU tar, and surely others, have been ported to Windows. Check within
the DJGPP and Cygwin projects.

--
François Pinard http://www.iro.umontreal.ca/~pinard
Jul 18 '05 #2
"Jay Donnell" <ja********@yahoo.com> wrote in message
news:a6**************************@posting.google.c om...
Is there a way to use the tarfile module to recursively compress the
contents of a directory and maintain the directory structure in the
tar archive?

Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
directory)
works great on linux, but I need this script to work on windows as
well :(


Starting from Python 2.3 there is a tarfile module in the stdlib
http://docs.python.org/lib/module-tarfile.html

Adonis
Jul 18 '05 #3
You can use os.walk (or os.path.walk for older versions of Python) to
recurse a directory tree. Here's a simple script to use tarfile and
os.walk:

import tarfile, sys, os

t = tarfile.TarFile(sys.argv[1], "w")
for f in sys.argv[2:]:
for dirpath, dirnames, filenames in os.walk(f):
for f in filenames:
f = os.path.join(dirpath, f)
print "Adding", f
t.add(f)

t.close()

Here's a sample session with it:
* Creating a simple directory structure
$ mkdir a
$ touch a/file.txt
$ mkdir a/subdir
$ touch a/subdir/subfile.txt

* Invoking the script
$ python ~/mktar.py test.tar a
Adding a/file.txt
Adding a/subdir/subfile .txt

* Checking on the results
$ tar tvf test.tar
-rw-rw-r-- jepler/jepler 0 2004-08-17 21:00:57 a/file.txt
-rw-rw-r-- jepler/jepler 0 2004-08-17 21:01:03 a/subdir/subfile.txt

I suspect that to get compressed output would involve use of gzip.open
and the 3-argument TarFile constructor, something like
import gzip
g = gzip.open(sys.argv[1], "w")
t = tarfile.TarFile(sys.argv[1], "w", g)
...
indeed, this seems to work for me.
$ python ~/mktargz.py test.tar.gz a
Adding a/file.txt
Adding a/subdir/subfile.txt
$ file test.tar.gz
test.tar.gz: gzip compressed data, was "test.tar", max compression

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBIrm1Jd01MZaTXX0RAmjCAKCpkTSIzUwPrrEbNOAZGe QAJ1TZlACgob8E
QGka1Q/r08XFFKnUcY+lJMo=
=A2fD
-----END PGP SIGNATURE-----

Jul 18 '05 #4
On Tue, 17 Aug 2004 21:06:45 -0500, Jeff Epler wrote:
You can use os.walk (or os.path.walk for older versions of Python) to
recurse a directory tree. Here's a simple script to use tarfile and
os.walk:
[snip]


Far too complicated... tarfile.py is rather high-level:

import tarfile

tar = tarfile.open(filename, "w:gz")
tar.add(directory)
tar.close()

The add() method is recursive by default. More information and examples
here: http://docs.python.org/lib/module-tarfile.html

--
Lars Gustäbel
la**@gustaebel.de

Jul 18 '05 #5
> import tarfile

tar = tarfile.open(filename, "w:gz")
tar.add(directory)
tar.close()

The add() method is recursive by default. More information and examples
here: http://docs.python.org/lib/module-tarfile.html


That doesn't maintain the directory structure. When you untar it all
the files are in the base directory (when I untar it on windows with
winzip).

I haven't tried jeff's suggestion yet, but I'll let ya'll know how
that goes.
Jul 18 '05 #6
At some point, ja********@yahoo.com (Jay Donnell) wrote:
import tarfile

tar = tarfile.open(filename, "w:gz")
tar.add(directory)
tar.close()

The add() method is recursive by default. More information and examples
here: http://docs.python.org/lib/module-tarfile.html


That doesn't maintain the directory structure. When you untar it all
the files are in the base directory (when I untar it on windows with
winzip).


Winzip is probably broken? It works for me using GNU tar on Linux.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #7
On Wed, 18 Aug 2004 15:13:29 -0400, David M. Cooke wrote:

Winzip is probably broken? It works for me using GNU tar on Linux.

There was a bug report relating to tarfile & WinZip:
http://sourceforge.net/tracker/?grou...ail&aid=949052

Looks like it was closed today.

regards,
Richard
Jul 18 '05 #8
> > import tarfile

tar = tarfile.open(filename, "w:gz")
tar.add(directory)
tar.close()


This works perfectly on linux, but it wasn't working for me on windows
yesterday. I'll try it again the next time I'm on windows.
Jul 18 '05 #9

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

Similar topics

9
by: FISH | last post by:
Ever have one of those days when you're not sure if it's you who's gone mad, or the rest of the world? I have an Open Source project on SourceForge for communication with YSMG - Yahoo's IM...
2
by: Jignesh | last post by:
Can someone please tell me how to "import" a directory structure into a project in Microsoft Visual C++ .NET 2003. I have an extremeley complex directory structure consisting of thousands of...
4
by: shane | last post by:
From searching around, Ive seen this question asked numerous times, but havent seen a usable answer unfortuantly. What Im trying to do is something I would have thought would be quite common,...
8
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or...
1
by: soni29 | last post by:
when creating a project with namespace like: wrox.csharp.basics.overflowtest is it good practice to have the directory structure like that also: c:\wrox\csharp\basics\overflowtext.cs Also if it...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
2
by: SharepointKida | last post by:
Hi I am building an web based .NET Application which allows users to specify a directory location from it's machine, and when it press publish it uploads all the files and subfolders folders from...
0
by: Brent Clements | last post by:
I have been trying to determine the best way to setup a directory structure for my homegrown MVC application. What do you guys suggest? I am thinking about doing the following: | +-- app
0
by: hsheboul | last post by:
Thanks to Markus for pointing me to all the Apache stuff http://bytes.com/topic/html-css/answers/876342-automating-web-site-template My Web site is hosted on a shared Linux hosting, that runs the...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.