i have to execute a command use python subprocess .
by default , command is execute as working on the directory where the pytho开发者_JAVA百科n 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)
You can use os.chdir or you use the cwd parameter to Popen.
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
hi did u try changing the unix command
ls -l
to something like
ls -l /usr/share;
精彩评论