The Python Oracle

Subprocess changing directory

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Magical Minnie Puzzles

--

Chapters
00:00 Question
00:55 Accepted answer (Score 239)
01:56 Answer 2 (Score 91)
02:42 Answer 3 (Score 37)
03:09 Answer 4 (Score 29)
03:43 Thank you

--

Full question
https://stackoverflow.com/questions/2140...

Answer 1 links:
[suggested in @wim's answer]: https://stackoverflow.com/a/21407005/427...
[normally]: https://stackoverflow.com/q/2375003/4279
[the code example in @glglgl's answer is wrong]: https://stackoverflow.com/a/21406995/427...

Answer 2 links:
[subprocess.popen-constructor]: https://docs.python.org/3/library/subpro...

Answer 3 links:
[docs]: http://docs.python.org/2/library/subproc...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #subprocess

#avk47



ACCEPTED ANSWER

Score 261


What your code tries to do is call a program named cd ... What you want is call a command named cd.

But cd is a shell internal. So you can only call it as

subprocess.call('cd ..', shell=True) # pointless code! See text below.

But it is pointless to do so. As no process can change another process's working directory (again, at least on a UNIX-like OS, but as well on Windows), this call will have the subshell change its dir and exit immediately.

What you want can be achieved with os.chdir() or with the subprocess named parameter cwd which changes the working directory immediately before executing a subprocess.

For example, to execute ls in the root directory, you either can do

wd = os.getcwd()
os.chdir("/")
subprocess.Popen("ls")
os.chdir(wd)

or simply

subprocess.Popen("ls", cwd="/")



ANSWER 2

Score 101


To run your_command as a subprocess in a different directory, pass cwd parameter, as suggested in @wim's answer:

import subprocess

subprocess.check_call(['your_command', 'arg 1', 'arg 2'], cwd=working_dir)

A child process can't change its parent's working directory (normally). Running cd .. in a child shell process using subprocess won't change your parent Python script's working directory i.e., the code example in @glglgl's answer is wrong. cd is a shell builtin (not a separate executable), it can change the directory only in the same process.




ANSWER 3

Score 39


subprocess.call and other methods in the subprocess module have a cwd parameter.

This parameter determines the working directory where you want to execute your process.

So you can do something like this:

subprocess.call('ls', shell=True, cwd='path/to/wanted/dir/')

Check out docs subprocess.popen-constructor




ANSWER 4

Score 30


You want to use an absolute path to the executable, and use the cwd kwarg of Popen to set the working directory. See the docs.

If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.