简体   繁体   中英

running windows command line program from python with ini file argument

Im trying to run a windows command line application from a python script with a ini config file in the command which i suspect isnt passing when its executed.

The command is c:\\BLScan\\blscan.exe test.ini.

The ini file is the config file that the application needs to know what parameters to scan with.

This is the script im using

import subprocess
from subprocess import Popen, PIPE

cmd = '/blscan/blscan test.ini'

p = Popen(cmd , stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
print "Return code: ", p.returncode
print out.rstrip(), err.rstrip()

When I use subprocess.popen to call the application it doesnt look to be reading the ini file. The device line is an indicator that the tuner hasnt been identified from the ini file so the program is dropping to the default tuner.

Return code:  0
BLScan ver.1.1.0.1091-commited
Config name: .\test.ini
Device 0: TBS 6925 DVBS/S2 Tuner
Device number: Total Scan Time = 0.000s
Transponders not found ! 
>>> 

This is how it looks when run from the dos shell.

C:\BLScan>blscan test.ini
BLScan ver.1.1.0.1091-commited
Config name: .\test.ini
Scan interval 0
From 3400 to 3430 Mhz, Step 5 Mhz, Horizontal, Minimal SR 1000 KS, Maximal SR 10
0000 KS
3400 Mhz ...
3405 Mhz ...
3410 Mhz ...

Any advice would be appreciated

When you run this from the DOS shell, your current working directory is C:\\BLscan , as is obvious from the prompt you show:

C:\BLScan>blscan test.ini

You can also tell from the error output that it's definitely looking in the current working directory. (Some Windows programs will, eg, try the same directory as the executable… but you can't count on that, and this one does not.)

Config name: .\test.ini

So, if your current directory were not C:\\BLScan , it wouldn't work from the DOS shell either. Try this:

C:\BLScan>cd \
C:\>\BLScan\blscan test.ini

You will get the exact same error you're getting in Python.

If you can't rely on being in C:\\BLScan, you have to pass an absolute path. For example, this will work again:

C:\>\BLScan\blscan \BLScan\test.ini

Python is no different from the shell here. If you give it a relative path like test.ini , it will use the current working directory. So, you have the same two options:

os.chdir('/blscan')
p = subprocess.popen('blscan test.ini')

… or:

p = subprocess.popen(r'\BLScan\blscan \BLScan\test.ini')

Try passing the arguments to the subprocess.call as an array: subprocess.call(["/blscan/blscan.exe","test.ini"])

Also, based on the command line versus py output in your question, double check that your blscan.exe tool works even when your "working directory" is different. Maybe you need to needs to be in the same working directory where blscan.exe is located.

os.chdir("C:\\BLScan")

You most likely need pass the path to the ini as well as to the exe:

clst = [r'C:\blscan\blscan.exe', r'C:\blscan\test.ini']
p = Popen(clst, stdout=PIPE, stderr=PIPE)
# etc . . .

If you pass Popen a list, it will quote out the args correctly for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM