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

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12

    How to zip a file?


    Hello All!

    I had this idea about a script that would come in handy for me
    and maybe for somebody else. At this stage I am still learning
    and this will be beyond my knowledge. If somebody can help me
    out by showing how to do this? What i want is to search for all
    files in all directories and if found a file ending in txt extention
    RAR(winrar) this file and then delete the original file.

    Thanks
    Random
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    You can use os.path.walk() to walk through a directory tree, and os.path.splitext() to get the extension. See the docs for the os.path module here: http://web.pydoc.org/2.1/posixpath.html

    To zip the file up, you'll probably need the zipfile module:
    http://web.pydoc.org/2.2/zipfile.html

    Finally, to remove the old file, use os.unlink():
    http://web.pydoc.org/2.1/os.html
    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. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    Thanks Scorpions4ever!

    I have started doing what you sudgested
    up until the actual process of ziping the
    the file which did not work.
    Any advice?


    Code:
    import os.path, zipfile
    
    def action(arg, dirname, file_list):
        for file in file_list:
            split = os.path.splitext(file)
            if (split)[1] == '.txt':
                z = zipfile.ZipFile(split[0]+ '.zip', "w")
                z.close()
    
    root = 'D:/new folder'
    arg = None
    os.path.walk(root, action, arg)
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You might find os.walk() easier to work with than os.path.walk() as well as being faster , also you can check the file extension for the filenames much easier using the str.endswith method i.e.

    Code:
    if file.endswith('.txt'):
        ...
        do whatever with the file
        ...
    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    arg you read my mind!! Mr you told me that built in func good you told him as well :P

IMN logo majestic logo threadwatch logo seochat tools logo