使用pyinstaller打包用于python可执行文件的.txt文件[复制]

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

这个问题在这里已有答案:

我在同一个目录中使用其他.txt文件构建了一个刽子手游戏,其中包含hangman游戏中的单词bank,这些是从hangman.py文件中打开的,当我使用pyinstaller打包我使用path\to\pyinstaller.exe hangman.py --onefile但这个不允许在可执行文件中使用.txt文件。如何打包.txt文件以便可执行文件正常运行?

python pyinstaller packaging python-packaging
1个回答
0
投票

您可以使用

pyinstaller --add-data 'path/to/file.txt:path/inside/exe' hangman.py

或者用文件列表编辑spec文件,即

added_files = [
     ( 'src/README.txt', '.' )
     ( '/mygame/sfx/*.mp3', 'sfx' ),
     ]
a = Analysis(...
     datas = added_files,
     ...
     )
© www.soinside.com 2019 - 2024. All rights reserved.