简体   繁体   English

Pyinstaller 可执行文件将文件保存到临时文件夹

[英]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.

相关问题 如何将多个文件夹层次结构中的所有 python 文件导入单个 pyinstaller 可执行文件? - How can I import all my python files in multiple folder hierarchy into a single pyinstaller executable? Pyinstaller-从最终可执行文件中排除文件 - Pyinstaller - Exclude files from final executable PyInstaller 可以将共享的 object 文件打包成可执行文件吗? - Can PyInstaller pack shared object files into executable? python PyInstaller创建的可执行文件无法运行exe错误:找不到Temp \\\\ _ MEI175682(数据文件) - python PyInstaller created executable failed to run the exe Error: cannot find Temp\\_MEI175682 (data file) 使用python删除临时文件夹中的所有文件 - Deleting all files in temp folder Using python pyinstaller build生成可执行文件后添加文件的位置 - Location of the added files after the executable file is generated by pyinstaller build 使用 pyinstaller 在可执行文件中包含图像文件 - 冲突源 - Including image files inside executable using pyinstaller - conflicting sources 如何使用 pyinstaller 将多个 .py 文件构建为单个可执行文件? - How to build multiple .py files into a single executable file using pyinstaller? 如何在可执行文件中使用 pyinstaller 包含特定的 .jar 文件? - How to include specific .jar files with pyinstaller in executable file? Pyinstaller可执行文件保持打开状态 - Pyinstaller executable keeps opening
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM