hadi Parvaz
hadi Parvaz

Reputation: 1

Executing some simple command in Command prompt using Python

I need to execute the simple command below in windows 7 command prompt using Python26.

cd C:\Python26\main project files\Process

C:\Aster\runtime\waster Analysis.comm

It runs a FEM simulation and I tried it manually and it worked well. Now, I want to automate the write procedure using Python26.

I studied the other questions and found that the os.system works but it didn't. Also I saw subprocess module but it didn't work.

Upvotes: 0

Views: 301

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 602715

The current directory is a process property: Every single process has its own current directory. A line like

os.system("cd xyz")

starts a command interpreter (cmd.exe on Windows 7) and execute the cd command in this subprocess, not affecting the calling process in any way. To change the directory of the calling process, you can use os.chdir() or the cwd keyword parameter to subprocess.Popen().

Example code:

p = subproces.Popen(["C:/Aster/runtime/waster", "Analysis.comm"],
                    cwd="C:/Python26/main project files/Process")
p.wait()

(Side notes: Use forward slashes in path names in Python files. You should avoid os.system() and passing shell=True to the function in the subprocess module unless really necessary.)

Upvotes: 2

Related Questions