linux - 我怎样才能让 subprocess 或 Popen 显示它刚刚运行的命令行?

标签 linux python-3.x subprocess popen

目前,我有一个看起来像下面这样的命令:

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/

相关文章:

c++ - Linux/Windows 的 sleep 时间

linux - 在 Docker 容器外的 VPN (SNX) 内无法访问服务器

python - (Python 初学者)需要从类(class)开始

python - 无法使用 python 过滤器返回预期输出

python - 在windows上通过python调用使用linux子系统编译的程序

python - 如何使用python获取wlan设备名称?

python - 使用 youtube-dl 的子进程进行自动歌曲列表下载

linux - 如何获取/下载 ubuntu 的 yum 开发工具?

python - 使用每个条目实例变量删除列表中重复项的最快方法

C++ 如何在 Linux 和 Windows 上不等待按键 grip "Enter"来读取按键