目前,我有一个看起来像下面这样的命令:
my_command = Popen([activate_this_python_virtualenv_file, \
"-m", "my_command", "-l", \
directory_where_ini_file_for_my_command_is + "/" + my_ini_file_name], \
stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=False,
universal_newlines=False, cwd=directory_where_my_module_is)
我已经弄清楚如何访问和处理输出,处理 subprocess.PIPE
,并使 subprocess 执行一些其他巧妙的技巧。
但是,the standard Python documentation for subprocess
对我来说似乎很奇怪没有提到获取实际命令行的方法,因为 subprocess.Popen
将它从参数放在一起到 Popen
构造函数。
例如,也许 my_command.get_args()
或类似的东西?
是不是让命令行在 Popen
中运行应该很容易?
我可以自己将参数放在一起,而无需访问使用 Popen
运行的命令 subprocess
,但如果有更好的方法,我想知道它。
最佳答案
它是在 Python 3.3 中添加的。根据docs :
The following attributes are also available:
Popen.args
The args argument as it was passed to Popen – a sequence of program arguments or else a single string.New in version 3.3.
所以示例代码是:
my_args_list = [] # yourlist
p = subprocess.Popen(my_args_list)
assert p.args == my_args_list
关于linux - 我怎样才能让 subprocess 或 Popen 显示它刚刚运行的命令行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704882/