Search code examples
pythonpython-3.xfileio

Python 3.6.1 - PermissionError: [Errno 13] Permission denied shown when trying to unzip a file


I am facing a PermissionError while I am trying to extract a zip file. I have gone through a lot of discussion threads here on SO but still unable to solve my issue.

Currently I am working with Python 3.6.1 on a Windows 8 box. I have created a new directory through the following code:

import os,zipfile

newpath = 'C:\\home\\vivvin\\shKLSE'
#newpath = r'C:\\home\\vivvin\\shKLSE'
if not os.path.exists(newpath):
os.makedirs(newpath)

Next I have downloaded a zip file and saved into newpath directory.

Now I am trying to extract all the files (10 csv files) within the zip file to be extracted into the newpath directory. To achieve that I have written the following code:

import os,zipfile

newpath = 'C:\\home\\vivvin\\shKLSE'
path_to_zip_file = newpath
directory_to_extract_to = newpath
#zip_ref = zipfile.ZipFile(newpath, 'r')
zip_ref = zipfile.ZipFile(newpath, 'w')
zip_ref.extractall(newpath)
zip_ref.close()

But each time I am getting an error as:

Traceback (most recent call last):
File "C:/Users/AtechM_03/PycharmProjects/Webinar/SeleniumScripts/extract.py", line 6, in <module>
zip_ref = zipfile.ZipFile(newpath, 'w')
File "C:\Python\lib\zipfile.py", line 1082, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\home\\vivvin\\shKLSE'

I have observed the properties manually of the zip file and seems there is a Security message along with a Unblock button. As of now I am clueless how to Unblock it.Unblock button

Can anyone help me out please? Thanks in advance.


Solution

  • I had a similar problem trying to write to a file.

    The fix that worked for me:

    Right-click your PyCharm application and run it as administrator.