2
In [3]: pwd

Out[3]: u'/Users/aarcher/Desktop/scripts'


In [5]: subprocess.call(['mkdir', '-p', os.path.expanduser('~/file/path/name')])

Out[5]: 0

I verified in another terminal that it had created /Users/aarcher/file/path/name successfully, but unable to change to that directory, even when it returns 0:

In [7]: subprocess.call(['cd', os.path.expanduser('~/file/path/name')], shell=True)

Out[7]: 0

In [8]: pwd

Out[8]: u'/Users/aarcher/Desktop/scripts'

I am in unix box

idjaw
  • 25,487
  • 7
  • 64
  • 83
A. Archer
  • 85
  • 2
  • 10
  • 1
    When you spawn a subshell and change directory, once that child shell closes it is gone. Modifying the pwd in the subshell doesn't affect the parent. Maybe you want to change the PWD with `os.chdir`? https://docs.python.org/2/library/os.html . You can also do something similar (without a subprocess) by calling `os.mkdir`. Advantage of the `os` module is that it was intended to be portable (so you wouldn't need to know the underlying mechanism used by the OS) – Michael Petch Oct 07 '15 at 19:58

2 Answers2

9

subprocess.call() creates a new process. The cd works in that process, but when the process exits it won't affect the current process. This is how processes are designed to work.

If you need your script to change to a different directory you can use os.chdir which will change the directory for the current process.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • When I use os.chdir it does not result in being in a different directory after the program terminates. I suppose the command-line itself is a different process. – Brōtsyorfuzthrāx Oct 02 '21 at 05:29
  • @Brōtsyorfuzthrāx: _"When I use os.chdir it does not result in being in a different directory after the program terminates"_ - correct, that's how operating systems and shells work. A child process cannot affect a parent process. Another example of this is that a child process can't change environment variables in the parent process. – Bryan Oakley Oct 02 '21 at 05:42
  • Thanks. I found gspr's answer to a different question (https://stackoverflow.com/questions/3786678/change-working-directory-in-shell-with-a-python-script#3786928) which addresses a useful alternative approach (doing `os.chdir(myDir)` followed by such as `subprocess.run(["bash"])` to open a new shell in the desired directory, without changing the old one; typing `exit` can return to the old one, of course). – Brōtsyorfuzthrāx Oct 02 '21 at 05:51
  • Ooh. You can also have it run more code after you type exit (just by putting more code after `subprocess.run["bash"]`), but at the same time, you kind of have to do that, if you want to run more code in the old shell (so opening the new shell should be the last task that needs to be done, much of the time). – Brōtsyorfuzthrāx Oct 02 '21 at 06:00
4

for this task no a good idea to use suprocess. just use python os module try this::

In [12]: import os 

In [13]: os.getcwd()
Out[13]: '/home/najeeb'

In [14]: os.chdir('/home/najeeb/Desktop/project/')

In [15]: os.getcwd()
Out[15]: '/home/najeeb/Desktop/project'

In [16]: 

if now any problem you face please let me know!

Najeeb Choudhary
  • 396
  • 1
  • 3
  • 21
  • If you're in dir /home/najeeb and then you want to enter Desktop dir which is subdir of najeeb just use os.chdir('Desktop') and it will change the dir. – Saurabh Dange May 13 '21 at 19:52