8

At the moment I am running a bash command from within Python using the following method:

os.system(cmd)

However I need to run the command in a new shell/terminal. Does anyone know how to do this?

Thanks, Dan

pmod
11.1k1 gold badge40 silver badges56 bronze badges
asked Oct 12, 2012 at 11:16
1
  • By "new terminal" do you mean you want your window manager to create a new terminal window in which the command runs? Commented Oct 12, 2012 at 11:18

3 Answers 3

8

I am using the following method (this will also redirect stderr to stdout):

import subprocess 
cmd_line = "echo Hello!"
p = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = p.communicate()[0]
print out
answered Oct 12, 2012 at 11:20
Sign up to request clarification or add additional context in comments.

Comments

1

os.system() is deprecated in favour of :

import subprocess
print subprocess.check_output("command", shell=True)
answered Oct 12, 2012 at 11:17

1 Comment

does this actually make a new separete process or just a subshell? bcs subshells can have conflicts with file handes. ie using os.system() is bad since thats a subshell
1

Windows "WshShell", Google it, is the answer. My complete steps:

Install

1. pip install pywin32-221-cp36-cp36m-win_amd64.whl
2. python.exe pywin32_postinstall.py -install (DOS command line)

Run

3. import win32com.client
4. WshShell = win32com.client.Dispatch("WScript.Shell")
5. WshShell.run("cmd") 

WshShell.run() is what you need, there are many different ways to run. hidden window, new window, full screen, minimized, ... etc.

answered Oct 2, 2017 at 3:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.