Reputation: 15
How to change the current directory of the command window from which a python script is invoked
I am writing a python script, that it should change directory (cd) of the current shell, to a specific directory, based on the user input.
The problem is everytime this script ends, I am back again to the original path, without changing.
bash-4.2$ pwd
/home/<username>/scripts
bash-4.2$ ./enterFW.py -fw <fw>
/filer/syslog_ng/2019/<fw>/2019/01/28 #<--I print the new directory, and it should be correct
bash-4.2$ pwd
/home/<username>/scripts #<-however after the script ends, i am back again to the original path
bash-4.2$
I change directory, using subprocess.call(['cd', <new path>])
, or os.chdir(<new path>)
...all are the same.
Please check, and advise
Upvotes: 0
Views: 1437
Reputation: 486
The only way I can think of is actual workaround. Where you pick up the output of your script in current shell. Otherwise eveything gets executed in subshell.
import os
new_dir = "/home/cabox/workspace"
os.chdir(new_dir)
print os.getcwd()
In linux shell:
cabox@box-codeanywhere:~/workspace/cliRtStocks$ pwd
/home/cabox/workspace/cliRtStocks
cabox@box-codeanywhere:~/workspace/cliRtStocks$ a=`python myprogram.py`;cd $a
cabox@box-codeanywhere:~/workspace$ pwd
/home/cabox/workspace
cabox@box-codeanywhere:~/workspace$
Upvotes: 1