Mahakaal
Mahakaal

Reputation: 2165

python error with Popen while using subprocess

I have my python script this

var1 = subprocess.Popen("pwd | grep 'home' ");
print var1

But it is giving me error

Upvotes: 0

Views: 580

Answers (1)

Mark Longair
Mark Longair

Reputation: 467031

You need to add shell=True if you want the shell to interpret the pipe correctly:

var1 = subprocess.Popen("pwd | grep 'home' ", shell=True)

(Note that you don't need a semi-colon at the end of the line.) That might not do what you expect, though - that returns a Popen object so then you need to check whether var1.wait() returns 0 or not.

A much easier way, if you just want to find out if the current directory contains 'home', is to do:

if 'home' in os.getcwd():
    print "'home' is in the current working directory's path"

Upvotes: 6

Related Questions