Created
June 5, 2015 03:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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