我使用 Plotly 的 Dash 构建了一个独立的桌面应用程序。为了部署它,我使用 pyinstaller 创建可执行文件,并使用 Inno Setup 为用户创建安装向导来安装应用程序。当我使用 pyinstaller 创建可执行文件时,我没有使用一个文件选项,因为我有多个配置文件。
当使用 pyinstaller 可执行文件运行时,应用程序按预期工作。但是当我使用已安装的版本时,它不起作用。我的直觉表明这是一个写入权限问题 issue ;我的应用程序为中间步骤创建一些临时文件。我应该如何处理这个问题? Windows 上是否有一个临时文件夹,我可以将文件保存到该应用程序不需要特殊写入权限的位置?我应该默认授予对程序的写访问权限吗?
最佳答案
正如 @PanagiotisKanavos 在评论中所述,答案不是授予程序文件的写入权限。正确的实现方法是使用 python 的 tempfiles
模块。
import tempfile
def functionA():
temp_file = tempfile.TemporaryFile()
temp_file.write("hello world")
contents = temp_file.read()
temp_file.close()
def functionB():
temp_file = tempfile.NamedTemporaryFile()
file_path = temp_file.name
temp_file.close()
请注意,tempfile 在关闭时会删除临时文件。如果您打算在 Windows 平台上使用该文件,则必须先将其关闭,然后才能由其他进程读取。为此,请在实例化 TemporaryFile
对象时使用 delete=False
标志。请注意,您必须手动删除该文件。通过使用命名的临时文件并在使用完毕后删除该路径中的文件,可以轻松完成此操作。
import tempfile
def functionC():
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write("hello world")
temp_path = temp_file.name
temp_file.close()
os.system(f"echo {temp_path}")
os.remove(temp_path)
关于python - 我应该授予我的应用程序对程序文件的写入权限吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72188463/