0

I have my python script this

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

But it is giving me error

1
  • In addition to the questions above, what're you trying to do? There's probably a better (built-in) way to do it. Commented May 1, 2011 at 8:53

1 Answer 1

6

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"
Sign up to request clarification or add additional context in comments.

2 Comments

i get this <subprocess.Popen object at 0xb7ea0a6c>] . actually i am learing python and i don't know where to start . are there any tutorials which can help me doing linux tasks with python
magazine.redhat.com/2008/02/07/… (2nd link in a Google search for "python bash replacement")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.