我正在写一个简短的script.py
,它将更新archive.tar
。
tar归档文件的内容是一个文件:file.txt (Content: "Hello")
。
我使用的是7za.exe
,它是7zip的命令行版本,用于用另一个file.txt
更新file.txt (Content: "Hello world")
。我正在使用以下命令:
os.system("7za u archive.tar file.txt")
到目前为止,一切正常,但是我想使用PyInstaller从Python脚本创建可执行文件,因为并非每个使用此脚本的人都将安装python。
我的问题:是否可以将script.py
和7za.exe
合并为一个单独的.exe文件?
到目前为止,我还没有找到答案。
答案 0 :(得分:3)
为什么不简单地使用ZipFile库?
这可能很简单:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
如果您真的想使用第三方,那么没有,没有简单的方法可以做您想做的事情。唯一“干净”的方法是使用安装程序将可执行文件打包在一起,就像可以使用Innosetup
创建的那样注意:您应该更喜欢使用子流程,而不是
os.system
,请参见https://docs.python.org/fr/3/library/subprocess.html#replacing-os-system
更新:对于tarfile,也许这些可以帮助您:
How to append a file to a tar file use python tarfile module?
How can files be added to a tarfile with Python, without adding the directory hierarchy?
也许您需要解压缩/添加/重新压缩,但是您应该能够在内存中完成它。