0

When I use os.system() or subprocess.run() with commands I want to run in the brackets, it runs it like typing the command in a command prompt or terminal.

But for example I were to do cd <my directory> to change my directory, and then afterwards I do a cd to display the current directory of the terminal, it would just always go back to the default directory.

Like I started on C:\Users\MYNAME, and I were to do a cd Desktop to go change directory to desktop, I expect it to change directory to C:\Users\MYNAME\Desktop, which it does in that instance, but when I use the command again, it would bring me back to C:\Users\MYNAME

I want it to be able to keep the shell being sort of "active", so when I use my python program to run cmd scripts, it would continue from the last instance, instead of like starting a new one.

(SORRY THIS IS MY FIRST TIME USING STACKOVERFLOW, I DON'T REALLY KNOW HOW TO TYPE THINGS HERE :D:D:D)

Here is my code:

import os, subprocess
while True:
    try:
        try:
            subprocess.run(usr_input, shell= True) # Or os.system(usr_input)
        except Exception as e:
            print(f'error:{e}')

        usr_input = input('INPUT:')
    except Exception as e:
        print(e)
JYU115
  • 1
  • 1
  • The command __CD__ is an internal command of Windows command processor `cmd.exe`. So executed is in background `%ComSpec% /c cd C:\Users\MYNAME\Desktop` with using deprecated `os.system` or its replacement `subprocess.run` with `shell = True`. Each process (running executable) has its own current directory. So the command __CD__ works for started `cmd.exe`, but that has no effect on current directory of `python.exe` which is executing the Python script. – Mofi Jun 12 '20 at 09:40
  • I am not a Python programmer and so entered in search box at top of every Stack Overflow page `[python] change current directory` and the second link on [results page](https://stackoverflow.com/search?q=%5Bpython%5D+change+current+directory) is [How do I change the working directory in Python](https://stackoverflow.com/questions/431684/)? – Mofi Jun 12 '20 at 10:01
  • Python is a much more powerful script interpreter than Windows command processor. So if you are writing a Python script which executes `cmd.exe` to run a batch file, you do definitely something wrong. Everything done with `cmd.exe` using a batch file can be done also with `python.exe` and appropriate Python code in a Python script. There is never, really never the need to use the two script interpreters `python.exe` and `cmd.exe` and other script interpreters like `cscript.exe` or `wscript.exe` or `powershell.exe` to get a task done on using powerful script interpreter Python. – Mofi Jun 12 '20 at 10:02
  • Example: The link at top of a Stack Overflow search with [\[python\] get desktop directory](https://stackoverflow.com/search?q=%5Bpython%5D+get+desktop+directory) is: [How to get Desktop location?](https://stackoverflow.com/questions/34275782/) I want to add that `%UserProfile%\Desktop` is just the default location. Every Windows user has the freedom to configure a different directory as Desktop directory, but that is rarely done. See [Making a directory not in the current directory](https://stackoverflow.com/questions/58514210/) for more details. – Mofi Jun 12 '20 at 10:02
  • Exta info: As described by the documentation for [subprocess.run](https://docs.python.org/3.8/library/subprocess.html) this method has also the argument `cwd` to the define the Current Working Directory for the executable run by Python. – Mofi Jun 12 '20 at 11:29
  • 1
    Every NT process has a Process Environment Block (PEB) that has info about the subsystem (e.g. usually the Windows subsystem); allocated heaps; loader state (activation contexts, API sets, loaded DLLs); active ANSI and OEM codepages; and the process parameters. The latter includes the process window station and desktop name in the desktop session (e.g. "WinSta0\Default"); console session handle (if any); console process group ID; initial window flags, position, and title; standard I/O handles (if any); image path; command line; working directory; and the environment variables (e.g. `PATH`). – Eryk Sun Jun 12 '20 at 18:28
  • 1
    When a child process is spawned, by default some of the parent process state and parameters are inherited to the child by *copying* them from the parent's PEB -- such as the window station and desktop name in the desktop session; the console session handle and standard handles (if attached to a console session); the console process group ID; the working directory; and environment variables. It would make no sense for the child process to be able to modify the environment of its parent. – Eryk Sun Jun 12 '20 at 18:34

0 Answers0