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)
If you want to change directory in the command prompt you have to use either
cdor a.batscript.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
.batscript works is that it is processed by the command shell that invokes it rather than by a child process.