2

I want to execute the command throughout the python and want to get output back

command is: ls /sys/class/net/ | sed -n '/e.*/p'

it returns the names of interfaces attached which starts with e

with terminal, I am getting the output as: eth0 eth1, which is expected

But with python, I am executing like this

out = subprocess.Popen(["ls", "/sys/class/net/", " | ", "sed -n '/e.*/p'"],
                        stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT,)
 stdout,stderr = out.communicate()
 print(stdout)

the expected output is eth0 eth1

but output:

ls: cannot access ' | ': No such file or directory\nls: cannot access 'sed -n '\''/e.*/p'\''': No such file or directory /sys/class/net/:\neth0\neth1\nlo\nwifi0\nwifi1\nwifi2\n

what is the problem here?

bufh
  • 3,153
  • 31
  • 36
rahul Kushwaha
  • 2,395
  • 7
  • 32
  • 64
  • 2
    Try with `subprocess.Popen(... , shell = Ture)` – Valentin M. Jan 30 '20 at 10:40
  • 1
    Does this answer your question? [How to use \`subprocess\` command with pipes](https://stackoverflow.com/questions/13332268/how-to-use-subprocess-command-with-pipes) – karolch Jan 30 '20 at 10:41
  • with 'shell - True` output is `b'#test.txt#\nDownloads\nbridge.py\ngeekfile.txt\nmicro-1.4.1\nt.xml\ntest.txt\n'` which is the conten of my current directorey, not `/sys/class/net/`.! – rahul Kushwaha Jan 30 '20 at 10:44
  • with `shell = True` need to pass the command as a single string, not it is working. – rahul Kushwaha Jan 30 '20 at 11:08

0 Answers0