Shell commands with a pipe in Python

paullb

I'm trying to run a shell command from python but as soon as I add a pipe, I get a "OSError: [Errno 2] No such file or directory" error. I tried a variety of things and even referenced grep directly at /bin/grep. Any idea what I'm doing wrong?

Works:

import subprocess
p = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
out, err = p.communicate()
print out

Doesn't Work:

import subprocess
p = subprocess.Popen(["ls | grep '20'"], stdout=subprocess.PIPE)
out, err = p.communicate()
print out

Displayed Error:

[OMITTED]$ python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    p = subprocess.Popen(["ls | grep '20'"], stdout=subprocess.PIPE)
  File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
[OMITTED]$

Version: Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Andriy Ivaneyko

The pipe | is a shell feature. If you want to use it, you have to use Popen with shell=True.

import subprocess
p = subprocess.Popen(["ls | grep '20'"], stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
print out

NOTE:

Warning Passing shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.

Source: subprocess.Popen

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute shell commands in Python

From Dev

Using groovy, how do you pipe multiple shell commands?

From Dev

Pipe multiple commands to socat reverse shell (network-namespaced)

From Dev

How can I pipe the output of *all* entered shell commands into another? (e.g. pipe everything into 'lolcat')

From Dev

Python subprocess.Popen blocks with shell and pipe

From Dev

When pipe linux commands, how to avoid piping python prints

From Dev

Importing OS Commands into Python; how to pipe with a variable in the middle?

From Dev

Run linux shell commands in Python3

From Dev

Getting output of shell commands in Python (cron)?

From Dev

Python -- execution shell commands using a designated shells

From Dev

How to split a file using shell commands in python

From Dev

How to open adb shell and execute commands inside shell using python

From Dev

How to open adb shell and execute commands inside shell using python

From Dev

pipe timely commands to ssh

From Dev

Max Pipe Commands of Linux

From Dev

Pipe/redirect a group of commands

From Dev

pipe multiple commands to less

From Dev

pipe of tar and tree commands?

From Dev

Pipe 3 terminal commands

From Dev

Pipe for three commands

From Dev

Piping to head results in broken pipe in shell script called from python

From Dev

Python subprocess: pipe an image blob to imagemagick shell command

From Dev

Imitate unix shell pipe

From Dev

Pipe to multiple files in the shell

From Dev

Measure pipe throughput in the shell

From Dev

Implement pipe in my shell

From Dev

How to run commands on same TCL shell using Python

From Dev

Python subprocess - run multiple shell commands over SSH

From Dev

Python subprocess - run multiple shell commands over SSH

Related Related

HotTag

Archive