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.
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:
Then an xdir.cmd file:
Then I create a doskey alias:
The end result is that I can type
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.