Skip to content

Instantly share code, notes, and snippets.

@stevenswafford
Created June 5, 2015 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenswafford/5460611f3e4a878a5b88 to your computer and use it in GitHub Desktop.
Save stevenswafford/5460611f3e4a878a5b88 to your computer and use it in GitHub Desktop.
Demonstrates how to use zipfile to write some files into a zip archive.
import os
import zipfile
# List all files in the current directory
allFileNames = os.listdir( os.curdir )
# Open the zip file for writing, and write some files to it
myZipFile = zipfile.ZipFile( "spam_skit.zip", "w" )
# Write each file present into the new zip archive, except the python script
for fileName in allFileNames:
(name, ext) = os.path.splitext( fileName )
if ext != ".py":
print "Writing... " + fileName
myZipFile.write( fileName, os.path.basename(fileName), zipfile.ZIP_DEFLATED )
myZipFile.close()
print ""
# Open the file again, to see what's in it
myZipFile = zipfile.ZipFile( "spam_skit.zip", "r" )
for info in myZipFile.infolist():
print "Reading...", info.filename, info.date_time, info.file_size, info.compress_size
raw_input( '\n\nPress Enter to exit...' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment