python中使用os.system调用exe可执行文件传入参数问题请教

Ace_wgk 2011-09-13 11:27:47
如题,我在python代码中使用了os.system()来调用DBS1.exe的可执行文件(windows下),我的那个DBS1.exe文件双击运行时候“需要输入某一个xx.O文件的路径然后回车,再输入一个阈值然后回车”这样,DBS1.exe文件运行后就可以把xx.O文件转换成一个xx.raw的图片了。也就是说我的DBS1.exe文件需要两个参数,而且两个参数后面都是使用回车作为输入结束标志的。
我这样写代码:

if Flag1==1 and Flag2==1 and Flag3==1:
os.system(r'D:\DBS\DBS1.exe a[0] threshold')
else:
print "no"

其中的a[0]是存放的xx.O文件的路径,threshold自然就是阈值了,但是不能输出结果,我觉得好像是三者之间不应该用空格,我用\n也不对啊,像这样:os.system(r'D:\DBS\DBS1.exe a[0] /n threshold /n')
求教!谢谢各位了!另外我还想在a[0]后面加上一个.decode('utf-8')但是这样老给我报错,说语法错误...高手能不能一起帮我解决了!谢谢各位!
...全文
5989 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2011-09-13
  • 打赏
  • 举报
回复
不要用os.system。文档里写的很清楚。

os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

iambic 2011-09-13
  • 打赏
  • 举报
回复
别看那种中文垃圾文章。搜索到的中文资料80%都是垃圾。
Ace_wgk 2011-09-13
  • 打赏
  • 举报
回复
我有试过这个方法
subprocess.call('D:\DBS\DBS1.exe a[0] threshold')
但是cmd一闪而过,无输出。
我参考的是:
http://hi.baidu.com/kobeantoni/blog/item/a034bce9d0e01bdfd539c9a4.html
[Quote=引用 4 楼 iambic 的回复:]
你按照一楼的说的升级到subprocess了吗?
[/Quote]
Ace_wgk 2011-09-13
  • 打赏
  • 举报
回复
刚才您给的方法报语法错误。。。
os.system('D:\DBS\DBS1.exe str(a[0]) str(threshold)')
这样写的时候,cmd一闪而过,无xx.raw的输出[Quote=引用 5 楼 ccddr 的回复:]
os.system('D:\DBS\DBS1.exe' str(a[0]) str(threshold))
参数以字符串方式,空格隔开试试,
[/Quote]
Ace_wgk 2011-09-13
  • 打赏
  • 举报
回复
关于DBS1.exe的接口定义在这里:

int main(int argc, char *argv[])
{
int threshold = 0;
unsigned char * arr0 = 0;
int row0 = 0;
int col0 = 0;
unsigned char * arr1 = 0;
/*int row1 = 0;*/
int col1 = 0;

unsigned char * arr2 = 0;
/*int row2 = 0;*/
int col2 = 0;

unsigned char * arr3 = 0;
int row3 = 0;
int col3 = 0;

FILE * input = 0;
int inputLen = 0;

FILE * output = 0;

if( argc != 4 )
{
fprintf(stderr, "Usage:\t %s [input_file] [output_raw_name] [threshold]\n", argv[0]);
return 7;
}

if (!(input=fopen(argv[1],"rb")))
{
printf("Cannot open file %s.\n", argv[1]);
exit(7);
}

fseek(input, 0, SEEK_END);
inputLen = ftell(input);
rewind(input);
arr0 = CAST(unsigned char *, malloc(inputLen));
fread(arr0, sizeof(unsigned char), inputLen, input);
fclose(input);

arr1 = CAST(unsigned char *, malloc(162*640));

[Quote=引用 6 楼 kerrywangxy 的回复:]
你的D:\DBS\DBS1.exe程序具体怎么调用得看调用的接口是怎样定义的,不是你想当然的把参数往里面凑的。
[/Quote]
kerrywangxy 2011-09-13
  • 打赏
  • 举报
回复
你的D:\DBS\DBS1.exe程序具体怎么调用得看调用的接口是怎样定义的,不是你想当然的把参数往里面凑的。
CCDDR 2011-09-13
  • 打赏
  • 举报
回复
os.system('D:\DBS\DBS1.exe' str(a[0]) str(threshold))
参数以字符串方式,空格隔开试试,
iambic 2011-09-13
  • 打赏
  • 举报
回复
你按照一楼的说的升级到subprocess了吗?
Ace_wgk 2011-09-13
  • 打赏
  • 举报
回复
着急求助啊,大家有没有什么办法帮我!谢谢了!
Ace_wgk 2011-09-13
  • 打赏
  • 举报
回复
您好!感谢您的回复,我如下写代码:
os.system('D:\DBS\DBS1.exe' %(a[0],threshold))
仿佛不能传入参数。。用下面方法也不行,
os.system('D:\DBS\DBS1.exe' -a[0] -threshold))
报错:
Traceback (most recent call last):
File "C:\Python27\DBS.py", line 617, in OnFindMe
subprocess.call('D:\DBS\DBS1.exe' %a[0] %threshold)
TypeError: not all arguments converted during string formatting

如果您写过类似程序的话,还望指教!

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧