Im trying to change the directory that the user is in through a python script. I have a bash script that changes the directory and im trying to call in Python. When I type . ./bash.sh on the command line, it does what I want it to do, but i can't figure out a way to replicate this behavior when calling the script in python. Anybody know how to do this or a workaround that will actually change the directory, not just the directory of a subprocess?
Asked
Active
Viewed 29 times
1
-
2You can't. You could make a python script that writes a directory name to stdout, and also make a shell function that captures that output and `cd`s to it. But you can't directly change the parent process's working directory from a subprocess. – alani Oct 09 '20 at 16:08
-
@alani: I think you could paste that as an answer. – jsbueno Oct 09 '20 at 16:13
-
1@jsbueno, ...but since we have other Q&A entries in the knowledgebase teaching as much already (and showing _examples_ of a shell function that uses output of the Python script), such an answer would be duplicative. – Charles Duffy Oct 09 '20 at 16:13
-
1@jsbueno The accepted answer at the link that Charles Duffy has found does the sort of thing that I had in mind. – alani Oct 09 '20 at 16:15