Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Is it possible to change the Windows command line shell current directory without changing the actual current directory?

I'm using os.system() to do Windows command line shell executions. I would like to change the Windows cmd current directory. Here's one way of doing it:

os.chdir('newPath')

But chdir() will also change the actual Python current working directory. I don't want to change the actual Python working directory because I want other parts of my script to run in the original current working directory. What I want to change is only the Windows cmd current working directory. In other words: I want os.system() commands to run in one current working directory (Windows cmd current working directory) while anything else should run in another current working directory (the actual Python current working directory).

Here's another try to change only the Windows cmd current directory:

os.system('cd newPath')

However, that obviously doesn't work since right after the execution of the cd newPath command the Windows cmd current directory is reset (because I won't use the same Windows command shell in the next call to os.system()).

Is it possible to have a separate current working directory for the Windows cmd shell? (separate from the actual current working directory).

like image 530
snakile Avatar asked Feb 02 '11 23:02

snakile


People also ask

How do I change the current directory in Python?

To change the current working directory(CWD) os. chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.

How do you change the current path of the directory in the command line?

To change current working directory under the current drive, use command " cd new-path " (change directory).

Which module has a method for changing the current working directory in Python?

chdir() method in Python used to change the current working directory to specified path.

How do I change the path in Windows Shell?

If the folder you want to open in Command Prompt is on your desktop or already open in File Explorer, you can quickly change to that directory. Type cd followed by a space, drag and drop the folder into the window, and then press Enter. The directory you switched to will be reflected in the command line.


2 Answers

The subprocess module is intended to replace os.system.

Among other things, it gives you subprocess.Popen(), which takes a cwd argument to specify the working directory for the spawned process (for exactly your situation).

See: http://docs.python.org/library/subprocess.html

Example usage replacing os.system:

p = subprocess.Popen("yourcmd" + " yourarg", shell=True, cwd="c:/your/path")
sts = os.waitpid(p.pid, 0)[1]
like image 108
payne Avatar answered Oct 06 '22 12:10

payne


If it only has to work on Windows, one way might be:

os.system('start /d newPath cmd')
like image 25
Mikel Avatar answered Oct 06 '22 12:10

Mikel