PermissionDenied saving files via a pyinstaller executable, works in VS Code

81 Views Asked by At
import os
import time
from PIL import ImageGrab

IMAGES_DIR = 'Estudos3\images'

FILENAME_FORMAT = 'screenshot_{}.png'

def take_screenshot():
    filename = FILENAME_FORMAT.format(int(time.time()))

    filepath = os.path.join(IMAGES_DIR, filename)

    ImageGrab.grab().save(filepath)

take_screenshot()

This python code takes a print of the screen and puts it in images, it works perfectly when I run it through vscode, the problem in question is that when I use PyInstaller to transform it into .exe and run it with ./ex.exe it displays an error of PermissionError: [Errno 13] Permission denied: 'Estudos3\images\screenshot_1683919164.png'

1

There are 1 best solutions below

2
Logan On

I recommend checking bwoodsend answer here:

https://github.com/orgs/pyinstaller/discussions/5468

You may also need to run the command prompt as admin before running the executable depending on if the folder you are writing to has security permissions (e.g. only accessible by admin)