python - 我应该授予我的应用程序对程序文件的写入权限吗?

标签 python pyinstaller inno-setup plotly-dash

我使用 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/

相关文章:

python - 如何使用 xml.etree.ElementTree 提取 xml 属性

python - 导入数字 : No module named Numeric in F2PY 失败

python - Pyinstaller 错误 : Unrecognized arguments when passing a spec file

inno-setup - InnoSetup : How to pass a two dimensional string array to a function

inno-setup - InnoSetup ExtractTemporaryFile 导致严重延迟?

python - 如何在 opencv 中使用 matlab imadjust 函数?

python - 使用 keras 的 sk-learn API 时出错

tkinter - python 3.5 上带有 tkinter 应用程序的 Pyinstaller 问题

python - 使用 PyInstaller 检查的问题;可以获得类的来源,但不能获得函数

inno-setup - Inno 设置 "deleteafterinstall"标志 : What happens if file is locked?