fork download
  1. '''
  2. I want a function which returns:
  3. dict("file1.txt": list(<contents of file1>),
  4. "file2.txt": list(<contents of file2>),
  5. "file3.txt": list(<contents of file3>),
  6. "file4.txt": list(<contents of file4>))
  7.  
  8. For input:
  9. file.zip:
  10. outer\
  11. outer\inner1.zip:
  12. file1.txt
  13. file2.txt
  14. outer\inner2.zip:
  15. file3.txt
  16. file4.txt
  17. '''
  18.  
  19. from zipfile import ZipFile, is_zipfile
  20. from StringIO import StringIO
  21.  
  22. def extract_zip(input_zip):
  23. return StringIO(ZipFile(StringIO(input_zip)).extractall())
  24.  
  25. def recursive_zip_contents(input_zip):
  26. return [extract_zip(this_zip) for this_zip in extract_zip(input_zip) if is_zipfile(this_zip)]
  27.  
  28. def get_filename_list_pairs(input_zip):
  29. return {(key, (word for word in open(key, 'r')).read()) for key in recursive_zip_contents(input_zip)}
  30.  
  31. if __name__ == "__main__":
  32. print get_filename_list_pairs('file.zip')
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty