Home > Back-end >  Python .exe PermissionError: [Errno 13] Permission denied
Python .exe PermissionError: [Errno 13] Permission denied

Time:08-31

Here's the code for my reprex:

from PyPDF2 import PdfFileReader as rdr
from PyPDF2 import PdfFileWriter as wtr

import os

SELF_PATH = os.path.dirname(__file__)
file_name = 'pdf_file'

in_path = fr'{SELF_PATH}\{file_name}.pdf'
assert os.path.isfile(in_path)
input = open(in_path, 'r b')

reader = rdr(input, strict=False)
writer = wtr()

orientation = reader.getPage(0).get('/Rotate')

for pagenum in range(reader.numPages):
    page = reader.getPage(pagenum)
    page.rotateClockwise(180)
    writer.addPage(page)

out_path = fr'{SELF_PATH}\_{file_name}.pdf'
assert os.path.isfile(out_path)
output = open(out_path, 'wb')

writer.write(output)
output.close()
input.close()

From this, I used auto-py-to-exe to build a one-directory .exe. Then I opened cmd as an administrator, navigated to the .exe's folder and attempted to run the executable. Here's the output:

Traceback (most recent call last):
  File "tab.py", line 11, in <module>
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\paulo\\OneDrive\\Pastas\\Python\\Personal\\DocReader\\tab\\pdf_file.pdf'
[25204] Failed to execute script 'tab' due to unhandled exception!

I am all out of ideas at this point, and I'd appreciate any thoughts.

CodePudding user response:

I solved the issue. My antivirus was blocking file creation.

Additionally, I'd suggest sticking with python's standard syntax regarding files.

import os

SELF_PATH = os.path.dirname(__file__)
with open(f'{SELF_PATH}/{file_name}.pdf', 'wb') as output:
    output.write(output)
  • Related