简体   繁体   中英

Pyinstaller executable saves files to temp folder

I am having a problem where I can't save files in the executable directory. Specifically

it saves it to C:\Users\hp\AppData\Local\Temp\_MEI102682\screenie.png

Pyinstaller command: pyinstaller --onefile main.py

code:

 import pyautogui import os __dir__ = os.path.dirname(os.path.abspath(__file__)) + '\\' print("Taking screenshot...") pyautogui.screenshot().save(__dir__ + "screenie.png") print(f"Saved to: {__dir__}screenie.png\nPress enter to open the file.") input() os.system(__dir__ + "screenie.png")

This thread shows the correct way to get the executable path, Pyinstaller actually runs your code inside the temporary directory specified, not inside your executable folder.

Another path you can use is you current directory which should be the executable path on windows, using os.getcwd() , unless your program is called from a command window or other program.

Just don't use __file__ or __dir__ as they will contain relative path of file and the directory of the temp folder your program runs in, and not the path you were getting when you run the program using your python interpreter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM