简体   繁体   中英

Python zipfile: RuntimeError: Bad password for file

With python's zipfile module, I could not open a encrypted zip file and I found out that the compression type is 99. I could open it with WinZip but I would like to automate the procedure with python.

Should I consider using 7zip's command line or is there some way in zipfile module itself to solve the problem? Thanks!

RuntimeError                              Traceback (most recent call last)
<ipython-input-43-4c4765b40715> in <module>()

      3         print (info.filename, info.date_time, info.file_size, info.compress_type)
      4     myzip.setpassword(b'password')
      5     with myzip.open('641903.txt','r') as myfile:<-----
      6         print(myfile.readline()

641903.txt (2018, 6, 26, 11, 59, 50) 342 99

RuntimeError: Bad password for file '641903.txt'

You can open password-protected files by simply adding a third parameter

    with myzip.open('641903.txt','r', 'password') as myfile:
        print(myfile.readline() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM