![](/img/trans.png)
[英]How can I import all my python files in multiple folder hierarchy into a single pyinstaller executable?
[英]Pyinstaller executable saves files to temp folder
我遇到了無法將文件保存在可執行目錄中的問題。 具體來說
它將它保存到C:\Users\hp\AppData\Local\Temp\_MEI102682\screenie.png
Pyinstaller 命令: pyinstaller --onefile main.py
代碼:
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")
該線程顯示了獲取可執行路徑的正確方法,Pyinstaller 實際上在指定的臨時目錄中運行您的代碼,而不是在您的可執行文件夾中。
您可以使用的另一個路徑是當前目錄,它應該是 windows 上的可執行路徑,使用os.getcwd()
,除非您的程序是從命令 window 或其他程序調用的。
只是不要使用__file__
或__dir__
因為它們將包含文件的相對路徑和程序運行的臨時文件夾的目錄,而不是使用 python 解釋器運行程序時獲得的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.