繁体   English   中英

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

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

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