PermissionError: [Errno 13] Permission denied on Windows

Question:

I’m working on a python program that outputs information into an Excel file using the openpyxl library. When I go to save the information using “workbook.save(excel_file_path),” I get "PermissionError: [Errno 13] pathtooutput_file.xlsx" message and the program crashes before writing to the file.

I keep getting directed to this post, but it did not solve my problem: https://stackoverflow.com/a/36469464

  1. Running CMD.exe as an administrator, navigating to my “application.exe” file, and then running it does not work.
  2. I cannot create a shortcut or batch file to run it with “python.exe application.py” because it imports an external library that is not detectable when run like this. In addition, I plan on running this program on multiple VMs, and would like to avoid downloading and installing Python and the necessary libraries for each one. A single .exe file is the best solution for this.
  3. I have tried running the .exe file as an administrator by right-clicking on it and clicking “Run as administrator.” I have also tried checking the "Run this program as an administrator" check button in the "properties > compatibility > settings" menu.

I opened the .exe file’s "properties > security" tab, and it shows that the program should have the necessary permissions to write to a file. When I click the "Advanced" button in the security tab, the Access column has "full control" for each row. This is also the case with the parent directory the Excel file is being saved to.

Other things to note:

  1. The program creates a parent directory with os.makedirs() to put the Excel file into if the directory does not exist already. The parent directory is always created before the program crashes, and since the parent directory was made by the program, I’m guessing it would not deny write permissions to files contained within it.
  2. The Excel file being written to is not open (or it does not exist yet), so it is not because of conflicting file usage.
  3. The Excel file is located in a different directory from the .exe file. Both the output directory and the .exe file are both on the administrator account in two different Desktop subdirectories.
  4. The program works fine in PyCharm, but not in the .exe created from Pyinstaller.

My OS is Windows 10, but I have also had this same problem on my old Windows 7 computer involving a text file. Python 3.8 and Pyinstaller 4.2 are being used for this.

Asked By: Foobar

||

Answers:

Have you used forward slash in between or backslash?. It should have forward slash.

Answered By: Abrar Malekji

I had this problem too.

I just delete the python version downloaded from the Microsoft Store and re-installing it from the website resolves my issue.

Here you can donwload the last python version: https://www.python.org/downloads/

Answered By: Geert
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.