Changing the working folder of an invitation through a Python script

Is it possible to change the working directory of the Windows command line through a Python script?

eg.

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

I tried several things that do not 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 and observe these operations, we change the current working directory of the processes - this is a Python process, not a request for its execution.

Thanks.

UPDATE

The path I would like to change is dynamic (depending on what project I'm working on, the full path to changing the assembly location), so I wanted to encode the solution in Python, and not hack the Windows batch file.

UPDATE

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

+4
source share
6

Python script, : xdir.py

, xdir.py, Windows stdout:

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

xdir.cmd:

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

doskey:

doskey x=xdir.cmd $*

$ x subdir

subdir.

script I, , , , , ..

+2

, . python script, Windows, Windows?

, 99.9% , . , python.exe Windows cmd.exe, , Python, .

-, Windows API, Windows - , .

, , Python script :

  • Python script, -.
  • Python script , Python script cmd.exe , .
+3

script.

1 - Python, .BAT, CD.

2 - .BAT .

fancycd.bat

python figurethepath.py >temp.bat
temp.bat
+1

, (.. ) (.. ). , . ​​.

, bash , , , @S.Lott Windows:

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

, cd (), .

+1

subprocess.Popen() doc page , , .

cwd None, childs cwd . , , cwd.

, , , .

0
imoprt os
os.system("start cmd.exe /k \"cd /d c:\\windows\\system32 & python make_decision_change_dir.py\"")
0

Source: https://habr.com/ru/post/1704763/


All Articles