November 20th, 2003, 01:52 PM
How to zip a file?
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.
November 20th, 2003, 02:06 PM
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:
Finally, to remove the old file, use os.unlink():
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
November 20th, 2003, 04:01 PM
I have started doing what you sudgested
up until the actual process of ziping the
the file which did not work.
import os.path, zipfile
def action(arg, dirname, file_list):
for file in file_list:
split = os.path.splitext(file)
if (split) == '.txt':
z = zipfile.ZipFile(split+ '.zip', "w")
root = 'D:/new folder'
arg = None
os.path.walk(root, action, arg)
November 20th, 2003, 07:19 PM
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.
do whatever with the file
November 20th, 2003, 09:32 PM
arg you read my mind!! Mr you told me that built in func good you told him as well :P