0

i used the following code so to persist the directory change after the program exits but the drawback is that it opens a new sub shell , so each tim a run it a new subshell is opened

import os, subprocess
newpath="/usr/local/www/nginx"
oschdir(newpath)
subprocess.call(['bash'])

is there a wayout , i want to do this in FreeBSD 10.1,python3 , irrespective of the shell . Is there a way out.

Rajdeep Sharma
  • 463
  • 1
  • 5
  • 9
  • The subshell here is opened for `bash` not for the directory changing. I'm confused. Do you need to run `bash` or not? Running `bash` here isn't part of changing the working directory of the python script. – Etan Reisner Jul 27 '15 at 14:11
  • Running `bash` like this also does absolutely nothing as it has no input and no script to run so it should just immediately exit. – Etan Reisner Jul 27 '15 at 14:12
  • ...and if you passed `bash` a `cd` command, it would change its own directory, and then exit -- *not* changing the parent (Python) process's directory. – Charles Duffy Jul 27 '15 at 14:16
  • I wanted to persist the directory change after the program exits so i opened bash ,, if i dont do this ,the directory change doesn't persist , thats why . – Rajdeep Sharma Jul 27 '15 at 15:46
  • Why do you think that's true? (`os.chdir()` has effect on the immediate process whether or not a subprocess is invoked afterwards; try printing `os.getcwd()` with and without the bash call, and you'll see that it's the same either way). – Charles Duffy Jul 28 '15 at 01:44
  • but i reach to a changed directory after program exits – Rajdeep Sharma Jul 28 '15 at 08:19

0 Answers0