I was wondering if there was a way that I could take a file and compress or take a file and unzip it. What would the code for that be like?
Zipping and unzipping
Collapse
X
-
Tags: None
-
I think there are a lot of modules to do this, like tarfile, zipfile, bz2, and some other.Originally posted by psychofish25I was wondering if there was a way that I could take a file and compress or take a file and unzip it. What would the code for that be like? -
Yes, but how? I looked up zipfile but didn't understand it.Originally posted by ilikepythonI think there are a lot of modules to do this, like tarfile, zipfile, bz2, and some other.Comment
-
-
Hi,
Some time ago i wrote a script that downloads a zip archive and unpacks it. Here is a part of code that was taking care of extracting all files from the archive.
I hope this will be helpful ...Code:# open an archive file my_zipfile = zipfile.ZipFile(sampleZIPFilename,'r') # unzip all files from an archive for my_zippedfile in my_zipfile.namelist(): f = open(my_zippedfile,'w') f.write(my_zipfile.read(my_zippedfile)) f.close()
Best regards,Comment
-
Following is a simple application of zipfile.Zipfile :[code=Python]import zipfile, osOriginally posted by psychofish25Yes, but how? I looked up zipfile but didn't understand it.
def makeArchive(fil eList, archive):
try:
# ZipFile will accept a file name or file object
a = zipfile.ZipFile (archive, 'w', zipfile.ZIP_DEF LATED)
for f in fileList:
print "archiving file %s" % (f)
a.write(f) # (f, os.path.basenam e(f))
a.close()
return True
except: return False
if __name__== '__main__':
fileList = ['H:/TEMP/temsys/abc1.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc3.txt',
'H:/TEMP/temsys/abc4.txt'
]
arcfile_name = 'H:/TEMP/temsys/zipped_files.zi p'
if makeArchive(fil eList, arcfile_name):
print arcfile_name, "was created.\n"
# check the new archive file
f = zipfile.ZipFile (arcfile_name, 'r')
for info in f.infolist():
print info.filename, info.date_time, info.file_size, info.compress_s ize
f.close()
else:
print "There was an error"[/code]Comment
-
Okay, thanks for the help.Originally posted by bvdetFollowing is a simple application of zipfile.Zipfile :[code=Python]import zipfile, os
def makeArchive(fil eList, archive):
try:
# ZipFile will accept a file name or file object
a = zipfile.ZipFile (archive, 'w', zipfile.ZIP_DEF LATED)
for f in fileList:
print "archiving file %s" % (f)
a.write(f) # (f, os.path.basenam e(f))
a.close()
return True
except: return False
if __name__== '__main__':
fileList = ['H:/TEMP/temsys/abc1.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc3.txt',
'H:/TEMP/temsys/abc4.txt'
]
arcfile_name = 'H:/TEMP/temsys/zipped_files.zi p'
if makeArchive(fil eList, arcfile_name):
print arcfile_name, "was created.\n"
# check the new archive file
f = zipfile.ZipFile (arcfile_name, 'r')
for info in f.infolist():
print info.filename, info.date_time, info.file_size, info.compress_s ize
f.close()
else:
print "There was an error"[/code]Comment
Comment