Friday, August 8, 2008

Filenames, paths, .zip files and such in Python

It may be hard to believe but I've had troubles looking for a way how to split a path to a file into filename and the path to it. So here it is. Let's have a path such as this:
we want to separate a file name 'python.exe' from the path to it 'C:/Python25/'. With the help of a standard Python module os.path it is an easy task to do:

import os.path
mypath = 'C:/Python25/python.exe'
separated = os.path.split(mypath)

where 'separated' is a tuple with two items. First is the path, second is the filename.
Do you also want to separate file name from its extension (e.g. '.exe' in our case)? It's easy:

name,extension = os.path.splitext(separated[1])

Do you also want to reverse this process? Just call the os.path.join() function:

completefilename = os.path.join(separated[0],separated[1])

Finally, I also wanted to compress some files to save disk space. There is a standard Python library called zipfile which supports creating .zip files (it also requires another library called zlib for compression of the .zip archives). Usage is simple:

import zipfile
f = zipfile.ZipFile( '', 'w', compression = zipfile.ZIP_DEFLATED)

The 'ZIP_DEFLATED' option tells the ZipFile class to compress the data, otherwise no compression is done.

1 comment:

Alex said...

I had a lot of zip files on my PC. And once something happened with them. To my good fortune I quickly found out a good tool, which perhaps can decide different tasks - zip file repair.