Thread: unzip script

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0

    unzip script


    hello,
    I would be more than very grateful if you could help me on this point:

    I'm trying to program a little script that downloads (through ftp) and unzips automatically my .tar.gz files.

    -What i manage: downoad and save on the disk

    -The problem: i know the file, where it is but don't know what instruction to do to untar and gunzip it... and of course save it again.

    If any of you has the solution you would be my python hero, thanks!
    bye
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    You can probably use the zlib and tarfile modules:
    http://docs.python.org/lib/module-zlib.html
    http://docs.python.org/lib/module-tarfile.html

    Alternatively, you can build the command string and use os.system
    Code:
    import os
    
    cmdstr = "tar -zxvf " + filename + " -C /some/path"
    os.system(cmdstr)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Theres an article in devsheds python section on Zipping - written by me - where this doesn't really apply fully to tar.gz the basic idea is the same so maybe it would be useful.

    http://www.devshed.com/c/a/Python/Python-UnZipped/

    As for the FTP bit you should check out the ftplib module and or search this forum for FTp since the subject comes up every now and again

    http://www.python.org/doc/2.3.3/lib/module-ftplib.html

    Hope this helps some,

    Mark.
    Last edited by netytan; May 7th, 2004 at 12:48 AM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0
    thanks a lot! and thanks also for the reaction time, you were quick!
    i'll look at all the links you said and test it!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    According to the docs, the tarfile module can open gzipped tar files directly, so you do not even need to mess around with the gzip module. You could download & save the tar.gz file with ftplib, then do:

    (warning: untested code. Caveat Emptor)

    Code:
    import tarfile
    
    tar = tarfile.open('path/to/file', 'r:gz')
    for member in tar.getmembers():
      tar.extract(member, 'output/directory')
    Note: the tarfile module was added in Python 2.3, so it will not work if you have an older version.

    Dave - The Developers' Coach
    Last edited by DevCoach; May 7th, 2004 at 03:47 AM.

IMN logo majestic logo threadwatch logo seochat tools logo