我在我的代码中使用命令
pyinstaller -F --add-data="token;." Main.py
。
我希望能够更改令牌文件的内容,并且在重新运行它后它保持原样
我尝试正常修改文件,但因为它位于临时文件夹中,所以它只保留在此会话中
当您使用带有 -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()