views:

1735

answers:

5

Is it possible to change the Windows command prompt working directory via Python script?

e.g.

>> cd
>> c:\windows\system32
>> make_decision_change_dir.py
>> cd
>> c:\windows

I have tried a few things which don't work:

import os
os.chdir(path)


import os, subprocess
subprocess.Popen("chdir /D \"%s\"" %path, shell=True)


import os, subprocess
subprocess.Popen("cd \"%s\"" %path, shell=True)


import os, subprocess
subprocess.Popen("CD=\"%s\"" %path, shell=True)

As I understand it and observe these operations change the current processes working directory - which is the Python process and not the prompt its executing from.

Thanks.

UPDATE

The path I would want to change to is dynamic (based on what project I am working on, the full path to a build location changes) hence I wanted to code a solution in Python rather than hack around with a Windows batch file.

UPDATE

I ended up hacking a batch file together to do this ;( Thanks everyone.

+3  A: 

I'm not clear what you want to do here. Do you want a python script which you can run from a Windows command prompt which will change the working directory of the Windows command session?

If so, I'm 99.9% sure that's impossible. As you said yourself the python.exe process is a separate process from the Windows cmd.exe and anything you do in Python won't affect the Command prompt.

There may be something you can do via the Windows API by sending keystrokes to the Windows or something but it would be pretty brittle.

The only two practical options I can think of involve wrapping your Python script in a Batch file:

  1. Output your desired directory from the Python script, read the output in your Batch file and CD to it.
  2. Start your Python script from a batch file, allow your Python script to start a new cmd.exe Window and get the Batch file to close the original Command window.
Dave Webb
Thanks for confirming the bad news! I marked @Ned Batchelder as my answer since that is a next best solution for me. I did up your answer though ;-)
Oliver
A: 

The subprocess.Popen() doc page says a child process will be created for the sub-process, so any working directory changes will be local to that subprocess.

If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.

This will be the same for any changes done explicitly inside the subproceess, similar to the commands that appear in the question.

gimel
+1  A: 

One common solution is a two-part script.

Part 1 is Python, which creates a temporary .BAT file that contains the appropriate CD command.

Part 2 is the temporary .BAT file.

fancycd.bat

python figurethepath.py >temp.bat
temp.bat
S.Lott
+1  A: 

I have a Python script to make moving around a file tree easier: xdir.py

Briefly, I have an xdir.py file, which writes Windows commands to stdout:

# Obviously, this should be more interesting..
import sys
print "cd", sys.argv[1]

Then an xdir.cmd file:

@echo off
python xdir.py %* >%TEMP%\__xdir.cmd
call %TEMP%\__xdir.cmd

Then I create a doskey alias:

doskey x=xdir.cmd $*

The end result is that I can type

$ x subdir

and change into subdir.

The script I linked to above does much more, including remembering history, maintaining a stack of directories, accepting shorthand for directories, and so on.

Ned Batchelder
Ned, I'm going to try out your 'xdir' script which could be really useful to me and others in my team. Thanks.
Oliver
A: 

As people mentioned, child processes (i.e. your program) can't change the current working directory of a parent process (i.e. the terminal). This is why you need the two steps that everybody is describing. In most shells there's a way to make a macro or function to perform this two-step functionality.

For example, in bash, you can make a single alias to compute the path and change the current working directory, similar to what @S.Lott describes for Windows:

alias my_cd='TMP=`compute_path.py`; cd $TMP;'

Note that the cd command is still being interpreted in the parent process (the terminal), which has the ability to change its own current working directory.

cdleary