首页 > 解决方案 > 如何从 Python 在终端上执行多个命令

问题描述

目前正在尝试在 Ubuntu 上通过加密压缩几个文件。

我需要它们是.zip

我通常会做的是在终端中写:

zip -e secretDir.zip secretFile.txt

这通常会要求输入密码,例如:

Enter password: secret
Verify password: secret

除了实际上没有显示密码之外。这工作得很好。

但是我试图从我的 Python 脚本(3.7)中做到这一点。

我有subprocess.call(['zip', '-e', fileName + '.zip', fileName + '.txt'])

这很好用,但是我现在应该如何告诉 zip 密码是什么?

欢迎在压缩文件时可能进行加密的开源库。我已经听说过 chilkat 和 CkPython。

标签: pythonpython-3.xubuntuterminalzip

解决方案


您可以使用参数将密码直接传递给zip命令-p

subprocess.call(['zip', '-P', 'MyPassW0rd123', fileName + '.zip', fileName + '.txt'])

推荐阅读