Search code examples
pythonwindowsextractunzipzip

Permission error when unzipping files that are password protected


I am trying to unzip a number of files that are password protected but I keep getting some permission error. I have tried to perform this operation running vscode as an administrator but I am still getting the same error.

Here is the code:

input_file = ".\\pa-dirty-price-crawler\\folders"

import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
    zip_ref.extractall(input_file, pwd=b'qpsqpwsr')

Here is the error:

Traceback (most recent call last):
  File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
    with zipfile.ZipFile(input_file, 'r') as zip_ref:
  File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: '.\\pa-dirty-price-crawler\\folders'

I do not know of another library that can do this same operation but if anyone has suggestions in regards to getting this fixed I would really appreciate it.

Edit:

When I try to specify the entire file path name as so:

input_file = "C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders"

import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
    zip_ref.extractall(pwd=b'qpsqpwsr')

I still get this error:

Traceback (most recent call last):
  File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
    with zipfile.ZipFile(input_file, 'r') as zip_ref:
  File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders'

Solution

  • It looks like you're passing a directory as the input. This is the likely problem, not that the zip is password protected.

    To extract a zip file, zipfile.ZipFile takes a zip file as an input not a directory.

    Therefore, your code needs two variables: an input zip file and an output directory:

    input_file = r".\pa-dirty-price-crawler\folders\myzipfile.zip"
    output_directory = r".\pa-dirty-price-crawler\folders"
    
    import zipfile
    with zipfile.ZipFile(input_file, 'r') as zip_ref:
        zip_ref.extractall(output_directory, pwd=b'qpsqpwsr')
    

    * note the use of r"string", this helps having to escape all your back slashes