[英]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
它将它保存到
C:\Users\hp\AppData\Local\Temp\_MEI102682\screenie.png
Pyinstaller command: pyinstaller --onefile main.py
Pyinstaller 命令:
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.该线程显示了获取可执行路径的正确方法,Pyinstaller 实际上在指定的临时目录中运行您的代码,而不是在您的可执行文件夹中。
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.您可以使用的另一个路径是当前目录,它应该是 windows 上的可执行路径,使用
os.getcwd()
,除非您的程序是从命令 window 或其他程序调用的。
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.只是不要使用
__file__
或__dir__
因为它们将包含文件的相对路径和程序运行的临时文件夹的目录,而不是使用 python 解释器运行程序时获得的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.