Posted By

throb on 05/01/10


Tagged

python zip


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

gartenstuhl


create a zipfile from an input file


 / Published in: Python
 

This creates a zipfile with the same name as the input file in the same location as the input file. Only does a single file.

  1. import zlib, zipfile, os
  2.  
  3. def zipme (inputFilename):
  4. fileName = os.path.basename(inputFilename)
  5. zipFileName = os.path.basename (os.path.splitext(fileName)[0]) + '.zip'
  6. zipFilePath = os.path.dirname (inputFilename)
  7. zFile = zipfile.ZipFile(zipFilePath + '/' + zipFileName ,'w')
  8. zFile.write (inputFilename, fileName, zipfile.ZIP_DEFLATED)
  9. zFile.close()
  10. if os.stat(zipFileName):
  11. print 'zipfile created : %s' % (zipFileName)
  12. return 1
  13. else :
  14. print 'Error creating zipfile : %s' % (zipFileName)
  15. return 0

Report this snippet  

You need to login to post a comment.