'C:/Python25/python.exe'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( 'myfile.zip', 'w', compression = zipfile.ZIP_DEFLATED)
f.write('mydata.txt')
f.close()
The 'ZIP_DEFLATED' option tells the ZipFile class to compress the data, otherwise no compression is done.