3

i have to execute a command use python subprocess .

by default , command is execute as working on the directory where the python script file is . such as

 subprocess.Popen(shlex.split("ls -l"))

will list the file in the directory of the script file where is .

but i want this command to execute in another directory ,say , B . how can i do it .

i try this , but faild:

env['PWD'] = "/data/a"
f = tempfile.TemporaryFile('w+b')
p = subprocess.Popen(shlex.split(cmd),stdout= f,stderr=f,env=env)
asked Mar 30, 2011 at 7:02

3 Answers 3

1

You can use os.chdir or you use the cwd parameter to Popen.

answered Mar 30, 2011 at 7:07
Sign up to request clarification or add additional context in comments.

Comments

0

ls -l list all items in current directory default. You could tell command ls -l which directory to list by using ls -l /your-path

answered Mar 30, 2011 at 7:19

Comments

0

hi did u try changing the unix command

ls -l 

to something like

ls -l /usr/share;
answered Mar 30, 2011 at 7:08

2 Comments

no , indeed i'm use execute the ls command , it's git command ,which can't put a path parameter.
can you say which command it is .. or use ; to combine two commands for eg: cd /var;ls

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.