简体   繁体   English

使用ini文件参数从python运行Windows命令行程序

[英]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. 我试图从带有ini配置文件的python脚本中运行python脚本运行Windows命令行应用程序,我怀疑该命令执行时没有通过。

The command is c:\\BLScan\\blscan.exe test.ini. 该命令是c:\\ BLScan \\ blscan.exe test.ini。

The ini file is the config file that the application needs to know what parameters to scan with. ini文件是应用程序需要知道要使用哪些参数进行扫描的配置文件。

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. 当我使用subprocess.popen调用应用程序时,它看起来并没有读取ini文件。 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. 设备行指示尚未从ini文件中识别出调谐器,因此程序将移至默认调谐器。

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. 这是从dos外壳运行时的外观。

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: 从DOS shell运行此命令时,当前工作目录为C:\\BLscan ,这在显示的提示中很明显:

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.) (例如,某些Windows程序将尝试与可执行文件相同的目录…,但您不能指望该目录,而这个目录则不能。)

Config name: .\test.ini

So, if your current directory were not C:\\BLScan , it wouldn't work from the DOS shell either. 因此,如果您当前的目录不是C:\\BLScan ,那么从DOS shell也将无法使用它。 Try this: 尝试这个:

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

You will get the exact same error you're getting in Python. 您将得到与在Python中完全相同的错误。

If you can't rely on being in C:\\BLScan, you have to pass an absolute path. 如果您不能依靠C:\\ BLScan,则必须通过绝对路径。 For example, this will work again: 例如,这将再次起作用:

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

Python is no different from the shell here. Python与此处的shell没什么不同。 If you give it a relative path like test.ini , it will use the current working directory. 如果您给它一个相对路径,例如test.ini ,它将使用当前的工作目录。 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"]) 尝试将参数作为数组传递给subprocess.call: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. 另外,根据您的问题中的命令行与py输出,即使您的“工作目录”不同,也请仔细检查blscan.exe工具是否可以正常工作。 Maybe you need to needs to be in the same working directory where blscan.exe is located. 也许您需要与blscan.exe所在的目录相同。

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

You most likely need pass the path to the ini as well as to the exe: 您最有可能需要将路径传递给ini以及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. 如果您通过Popen列表,它将为您正确引用args。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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