我如何修改 One File Pyinstaller EXXE 中包含的数据文件

问题描述 投票:0回答:1

我在我的代码中使用命令

pyinstaller -F --add-data="token;." Main.py
。 我希望能够更改令牌文件的内容,并且在重新运行它后它保持原样

我尝试正常修改文件,但因为它位于临时文件夹中,所以它只保留在此会话中

python pyinstaller data-files
1个回答
0
投票

当您使用带有 -F (或 --onefile)选项的 PyInstaller 时,PyInstaller 会将内容提取到一个临时文件夹中,该文件夹会在程序退出后删除,因此一段时间后您将无法进行更改。

# Define the persistent directory
persistent_dir = 
  os.path.join(os.path.expanduser('~'),'your_application_data')
  os.makedirs(persistent_dir, exist_ok=True)

# Define the path to the token file in the persistent directory
persistent_token_path = os.path.join(persistent_dir, 'token')

# Now, always use the token file from the persistent directory
with open(persistent_token_path, 'r') as token_file:
token = token_file.read()
© www.soinside.com 2019 - 2024. All rights reserved.