Sunday, 15 June 2014

Read zip files in python


We can read all the files without extracting zip file easily. and we can read line by line in zip files.

Script
try:
   root = zipfile.ZipFile("C:\Python27\Project\Rokar.zip", "r")
except:
   root = "." 
data=[]
for name in root.namelist()

       for line in root.read(name):
           data.append(line)
print ''.join(data)


or
try:
   root = zipfile.ZipFile("C:\Python27\Project\Rokar.zip", "r")
except:
   root = "."  

with root as z:

   with z.open('manage.py') as f:

       for line in f:

           print line

No comments:

Post a Comment