Reputation: 2106
I trying to change the path where I located in a python3
script using a bash command.
I use this small code but it doesn't work:
import subprocess
args = ['cd', '/foo/bar/xxx']
subprocess.Popen(args)
I also try to use subprocess.call()
and subprocess.run()
but it doesn't change the path
Upvotes: 0
Views: 42
Reputation: 568
By doing running those commands, you are launching a new process, which changes its directory to /foo/bar/xxx
, then exits. To affect the path of the parent program, use os.chdir like so:
import os
os.chdir('/foo/bar/xxx')
Fun fact: this is why bash and other shells have cd
as a builtin; any program equivalent to cd could only affect its own path.
Upvotes: 3