0

To open an application in Windows named DriveMaster, I am using subprocess.Popen:

subprocess.Popen(['C:\\Program Files (x86)\\ULINK DM2012 PRO NET\\v970\\DriveMaster.exe'])

Now, if I need to open DriveMaster with a script file loaded, what should I do? From windows command prompt or a windows batch file I run:

"C:\Program Files (x86)\ULINK DM2012 PRO NET\v970\DriveMaster.exe" /s:c:\Program Files (x86)\ULINK DM2012 PRO NET\v970\Scripts\ATA\SATA_TestBatch.srt

Now I need to open DriveMaster with the script file SATA_TestBatch.srt file. Please note there is '/s:' included in the command to load the script file.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
user3565150
  • 884
  • 5
  • 21
  • 49
  • Somewhat related: http://stackoverflow.com/questions/3730964/python-script-execute-commands-in-terminal –  Feb 20 '15 at 09:34

2 Answers2

1

The first thing that you can try is to run the command as is:

import subprocess

subprocess.check_call(r'"C:\Program Files (x86)\ULINK DM2012 PRO NET\v970\DriveMaster.exe" /s:c:\Program Files (x86)\ULINK DM2012 PRO NET\v970\Scripts\ATA\SATA_TestBatch.srt')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • But this involves, and indeed requires, the shell, which can easily be avoided here. – tripleee Feb 21 '15 at 10:28
  • @tripleee: **wrong**. Popen does *not* use the shell by default. – jfs Feb 21 '15 at 10:29
  • Note the use of double quotes around the executable's path. Without them `CreateProcess` scans `lpCommandLine` for whitespace from left to right to find the executable's path. For example, without quotes it tries `C:\Program`, `C:\Program Files (x86)`, `C:\Program Files (x86)\ULINK`, `C:\Program Files (x86)\ULINK DM2012`, `C:\Program Files (x86)\ULINK DM2012 PRO`, and finally `C:\Program Files (x86)\ULINK DM2012 PRO NET\v970\DriveMaster.exe`. If any of the latter doesn't exist it also tries appending a `.EXE` extension before moving on. – Eryk Sun Feb 23 '15 at 00:25
0

This should work :

subprocess.Popen([r'C:\\Program Files (x86)\\ULINK DM2012 PRO NET\\v970\\DriveMaster.exe', r'/s:c:\Program Files (x86)\ULINK DM2012 PRO NET\v970\Scripts\ATA\SATA_TestBatch.srt'])

reference: https://docs.python.org/3/library/subprocess.html

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence.

Jean Coiron
  • 632
  • 8
  • 24
  • 1
    it causes `SyntaxError`. Use raw-string literals for Windows paths: `r'...\U..'` – jfs Feb 21 '15 at 06:00
  • An error is raised you're right. It's actually a FileNotFoundError. My bad, I rarely use Windows :) . Answer edited, thank you Sebastian. Tested using Python3.4 for Windows : `subprocess.Popen(['C:\test.bat', '--my-test-arg']` . Test.bat contains `echo %1%`, and it prints `--my-test-arg` – Jean Coiron Feb 23 '15 at 13:33
  • 1
    No. If the path has `'\U'` in it then SyntaxError is raised – jfs Feb 23 '15 at 13:35