get file list of files contained in a zip file

前端 未结 3 1614
慢半拍i
慢半拍i 2020-12-13 07:57

I have a zip archive: my_zip.zip. Inside it is one txt file, the name of which I do not know. I was taking a look at Python\'s zipfile

相关标签:
3条回答
  • 2020-12-13 08:51

    What you need is ZipFile.namelist() that will give you a list of all the contents of the archive, you can then do a zip.open('filename_you_discover') to get the contents of that file.

    0 讨论(0)
  • 2020-12-13 08:52
    import zipfile
    
    zip=zipfile.ZipFile('my_zip.zip')
    f=zip.open('my_txt_file.txt')
    contents=f.read()
    f.close()
    

    You can see the documentation here. In particular, the namelist() method will give you the names of the zip file members.

    0 讨论(0)
  • 2020-12-13 08:56
    import zipfile
    
    zip = zipfile.ZipFile('filename.zip')
    
    # available files in the container
    print (zip.namelist())
    
    
    # extract a specific file from zip 
    f = zip.open("file_inside_zip.txt")
    content = f.read()
    # save the extraced file 
    f = open('file_inside_zip.extracted.txt', 'wb')
    f.write(content)
    f.close()
    
    0 讨论(0)
提交回复
热议问题