0

Im trying to use python to run cmd.exe and thereby running commands like cd C:\name..... and executing other programs from the cmd what I have so far is.

os.system("cmd.exe").
os.system("cd C:\name\first\second").

When I try to run three other commands a new cmd window replaces the old one and the commands dont work since they need to be consecutively after each other.I already tried the above code and need help running the next three. Also can you explain what suproccess are.

3
  • This may be a workaround, but instead of using "cd", why don't you just do `os.system("C:\name\first\second\cmd.exe") etc. Commented Jun 18, 2014 at 20:02
  • Depends a bit on what exactly you want to achieve, but have a look at this answer: stackoverflow.com/questions/89228/… Commented Jun 18, 2014 at 20:03
  • It's unclear exactly what you mean by this, "... the commands dont work since they need to be consecutively after each other," since you are quite literally running the commands one after the other. For why cd specifically doesn't work see my answer below. Commented Jun 18, 2014 at 20:07

1 Answer 1

2

See my answer to this recent question for why os.system("cd WHEREVER") does not do what you expect.

Briefly, when you run os.system('cd WHEREVER') you are creating a new command shell which has its own idea of the current directory. This change in the current directory will be entirely "forgotten" on subsequent calls to os.system(). You need to change the current directory in the parent process (the script) with os.chdir('WHEREVER') in order to retain the change for subsequent os.system() calls.

Sign up to request clarification or add additional context in comments.

6 Comments

Did you read my answer and the examples from the other question?
What do you mean "the for loop"? If you have a question about that specific example, add a comment there.
Besides changing the directory is there any way to keep the current command line while passing arguments with python
I don't know what "keep the current command line" means either. As suggested above, you should post a complete example, explain what it is trying to do, and what it actually does.
When you use os.system you open a new command shell each time so is there a way to open the shell once and pass arguments later in python
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.