7

I am using os.startfile('C:\\test\\sample.exe') to launch the application. I don't want to know the application’s exit status and I just want to launch the exe.

I need to pass the argument to that exe like 'C:\\test\\sample.exe' -color

Please suggest a method to run this in Python.

1
  • use os.system("command") Commented Oct 31, 2014 at 17:53

2 Answers 2

11

You should use the subprocess module instead of os.startfile or os.system in every case that I'm aware of.

import subprocess
subprocess.Popen([r'C:\test\sample.exe', '-color'])

You could, as @Hackaholic suggests in the comments, do

import os
os.system(r'C:\test\sample.exe -color')

But this is no simpler, and the docs for os recommend the use of subprocess instead.

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

5 Comments

subprocess is waiting for the new exe to complete but my objective is just to launch new exe. below code is not working. os.system(r'c:\\Program Files\\bin\\winx64\\sample.exe -b -color')
@Bala Ah, right, forgot that os.startfile opens in a new process. I've edited my answer; you should use subprocess.Popen if you don't want the call to block.
@Bala Note also: you should either use double slashes 'C:\\foo' OR raw strings r'C:\foo'. Not both. If you use both, you will have too many backslashes in your file paths. That is probably why the os.system call in your comment is failing.
The following options are not working. Please suggest: subprocess.Popen(['C:\data\ANSYS.exe -b -i cdb_wb.txt']) subprocess.check_call([r'C:\data\ANSYS.exe', '-b -i cdb_wb.txt'])
@Bala You should probably pass each flag and argument separately, so that your argument list looks something like [r'C:\data\ANSYS.exe', '-b', '-i', 'cdb_wb.txt']. If this doesn't work for you, please ask a new question.
0

Create a batch file sam_ple.bat with the following commands and arguments

cd C:\test\
start sample.exe -color

Then put sam_ple.bat in the same directory as your script.py file

Enter the following line of code in python to launch the exe:

os.startfile('.\sam_ple.bat')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.