2

I am trying to write a python script that will change my cwd to the desired directory. I was not able to do this task directly from python so I wrote a simple batch script to do that.

Changedir.bat

@echo off
chdir /D F:\cygwin\home\

If I execute the above script directly in my cmd it works fine but if I try to execute it with a python script nothing happens. My cwd remains same.

PythonScript.py

import shlex,subprocess

change_dir = r'cmd.exe /c C:\\Users\\test.bat'
command_change = shlex.split(change_dir)
subprocess.call(command_change)
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • If you want to change working directory because of some task you need to run that needs specific working directory then you'll find out how to do this in [Python: Is it possible to change the Windows command line shell current directory without changing the actual current directory?](http://stackoverflow.com/questions/4881312/) – Piotr Dobrogost Apr 01 '12 at 09:19
  • Related: [Perl change working directory of caller](http://stackoverflow.com/questions/5955389/), with interesting statement *“Not possible” is not super-strictly true.* – Piotr Dobrogost Apr 01 '12 at 09:29

3 Answers3

5

Of course this can't work, because subprocess.call is spawning whole new process for your script. This executes the script in a completely separate environment.

tchap
  • 1,047
  • 8
  • 10
  • So, than whats the alternative.? See my comment to @alberge answer. – RanRag Mar 31 '12 at 20:41
  • I only know Unix processes, but I guess this works more or less the same everywhere. If you really understand processes, then you must discover that this cannot be done easily (probably not at all). What you want is to start python (= new process = its own environment = its own cwd), then change cwd in there (in python process), then exit, return to your cmd process and expect it to have its cwd changed. – tchap Mar 31 '12 at 21:08
2

If you want to change directory in the command prompt you have to use either cd or a .bat script.

You can't get another process (i.e. Python) to do it because changes to the current directory, made in another process are not reflected back to the parent process. The reason the .bat script works is that it is processed by the command shell that invokes it rather than by a child process.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

You could try this. It works in Linux to change the CWD of the current shell. It is horrible.

def quote_against_shell_expansion(s):
    import pipes
    return pipes.quote(s)

def put_text_back_into_terminal_input_buffer(text):
    # use of this means that it only works in an interactive session
    # (and if the user types while it runs they could insert
    #  characters between the characters in 'text')
    import fcntl, termios
    for c in text:
        fcntl.ioctl(1, termios.TIOCSTI, c)

def change_shell_working_directory(dest):
    put_text_back_into_terminal_input_buffer("cd "+quote_against_shell_expansion(dest)+"\n")
mrdiskodave
  • 353
  • 2
  • 4