{"id":7776,"date":"2023-11-02T20:38:27","date_gmt":"2023-11-02T15:08:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=7776"},"modified":"2023-11-10T12:12:33","modified_gmt":"2023-11-10T06:42:33","slug":"python-how-to-get-the-list-of-all-files-in-a-zip-archive","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-get-the-list-of-all-files-in-a-zip-archive\/","title":{"rendered":"Python : How to get the list of all files in a zip archive"},"content":{"rendered":"
In this article we will learn about various ways to get detail about all files in a zip archive like file\u2019s name, size etc.<\/p>\n
ZipFile<\/span><\/span> class from Here a list of ZipInfo <\/em>objects is returned where each object has certain information like name, permission, size etc.<\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" How to get the list of all files in a zip archive in Python In this article we will learn about various ways to get detail about all files in a zip archive like file\u2019s name, size etc. How to find name of all the files in the ZIP archive using ZipFile.namelist() : ZipFile class …<\/p>\nzipfile<\/code> module provide a member function i.e.
ZipFile.namelist()<\/code> to get the names of all files present it.<\/p>\n
from zipfile import ZipFile\r\ndef main():\r\n # To Create a ZipFile Object and load Document.zip in it\r\n with ZipFile('DocumentDir.zip', 'r') as zip_Obj:\r\n # To get list of files names in zip\r\n fileslist = zip_Obj.namelist()\r\n # Iterate over the fileslist names in given list & print them\r\n for ele in fileslist:\r\n print(ele)\r\nif __name__ == '__main__':\r\n main()\r\n<\/pre>\n
Output :\r\nDocumentDir\/doc1.csv\r\nDocumentDir\/doc2.csv\r\nDocumentDir\/test.csv<\/pre>\n
Find detail info like name, size etc of the files in a Zip file using ZipFile.infolist() :<\/h3>\n
ZipFile<\/code><\/code>\u00a0class from
zipfile<\/code> module also provide a member function i.e.
ZipFile.infolist()<\/code> to get the details of each entries present in zipfile.<\/p>\n
from zipfile import ZipFile\r\ndef main():\r\n # To Create a ZipFile Object and load Document.zip in it\r\n with ZipFile('DocumentDir.zip', 'r') as zip_Obj:\r\n # Get list of ZipInfo objects\r\n fileslist = zip_Obj.infolist()\r\n # Iterate of over object\u2019s list and also to access members of the object\r\n for ele in fileslist:\r\n print(ele.filename, ' : ', ele.file_size, ' : ', ele.date_time, ' : ', ele.compress_size)\r\nif __name__ == '__main__':\r\n main()\r\n<\/pre>\n
Output :\r\nDocumentDir\/doc1.csv :\u00a0 2759\u00a0 :\u00a0 (2021, 01, 03, 21, 00, 02)\u00a0 :\u00a0 2759\r\nDocumentDir\/doc2.csv :\u00a0 2856\u00a0 :\u00a0 (2021, 01, 25, 13, 45, 58)\u00a0 :\u00a0 2856\r\nDocumentDir\/test.csv\u00a0 :\u00a0 3458\u00a0 :\u00a0 (2021, 02, 20, 20, 20, 41)\u00a0 :\u00a0 3458<\/pre>\n
Details of ZIP archive to std.out using ZipFile.printdir() :<\/h3>\n
ZipFile<\/code><\/code>\u00a0class from
zipfile<\/code> module also provide a member function i.e.
ZipFile.printdir()<\/code> which can print the contents of zip file as table.<\/p>\n
from zipfile import ZipFile\r\ndef main():\r\n # To Create a ZipFile Object and load Document.zip in it\r\n with ZipFile('DocumentDir.zip', 'r') as zip_Obj:\r\n zip_Obj.printdir()\r\nif __name__ == '__main__':\r\n main()\r\n<\/pre>\n
Output :\r\nFile Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Modified\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Size\r\nDocumentDir\/doc1.csv\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2021-01-03 21:00:02\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2759\r\nDocumentDir\/doc2.csv\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2021-01-25 13:45:58\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2856\r\nDocumentDir\/test.csv\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 2021-02-20 20:20:41\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3458<\/pre>\n