[英]Executing linux command using python subprocess
I have a requirement where I need to extract port number from a file example.ini
, this file is in linux directory.我有一个要求,我需要从文件
example.ini
中提取端口号,该文件位于 linux 目录中。
Now when I am executing below command from CLI its giving exact result which I want现在,当我从 CLI 执行以下命令时,它会给出我想要的确切结果
$ cat path/example.ini | grep -i variable | cut -d '=' -f 2
however I want run this command using python script using subprocess.run
但是我想使用 python 脚本使用
subprocess.run
运行此命令
I am getting below error我得到以下错误
Can someone please help me to execute this command in subprocess.run command.有人可以帮我在 subprocess.run 命令中执行这个命令吗?
I am executing in script我正在脚本中执行
subprocess.run(['cat', 'path', '|', 'grep -i variable', '|', 'cut -d "=" -f2'])
I am getting error: No such file or directory
我收到错误:
No such file or directory
why use cat with grep?为什么将 cat 与 grep 一起使用? grep -i variable path/example.ini |
grep -i 变量路径/example.ini | cut -d '=' -f 2
剪切 -d '=' -f 2
import subprocess
ps = subprocess.Popen(('grep', '-i', 'variable', 'path/example.ini'), stdout=subprocess.PIPE)
output = subprocess.check_output(('cut', '-d', '=', '-f', '2'), stdin=ps.stdout)
ps.wait()
print(output.decode("utf-8")) # bytes object to UTF8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.