通过python终端运行外部程序 [英] Running external program through python terminal

查看:165
本文介绍了通过python终端运行外部程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行程序(带有tcl文件的词干分析器)以读取txt文件并将结果保存到其他txt文件中.当我通过dos-windows终端运行命令时,它工作正常,但是当我通过os.system()通过python终端运行命令时,它返回1值,并且什么都没有发生. 这是代码:

i try to run a program( a stemmer with a tcl file) to read a txt file and save the result into an other txt file. When i run the command through dos-windows terminal it works fine, but when i run this through python terminal with os.system() it returns 1 value and nothing happens.. Here is the code:

>>>import os
>>>os.system('C:\Python27\Lib\site-packages\tclsh.exe -encoding utf-8     C:\Python27\Lib\site-packages\GreekStemmer.tcl in.txt out.txt')
>>>1

我猜为"1"意味着命令未成功执行?当我在dos-terminal中运行它时,它会创建带有结果的out.txt文件.但是这里没有.

I guess '1' means that the command didnt executed succesfully?? And when i run this in dos-terminal it creates the out.txt file with the result.But here not..

推荐答案

在我的计算机上,以下命令不起作用,因为反斜杠没有被解释.它们表示特殊的角色.

On my machine the following doesn't work, because the backslashes are not interpretted. They indicate special charachters.

import os
os.system('C:\bin\Tcl\bin\tclsh.exe')

您可以在字符串前添加一个r

You can add an r before the string

import os
os.system(r'C:\bin\Tcl\bin\tclsh.exe')

或使用加倍的后挡板

import os
os.system('C:\\bin\\Tcl\\bin\\tclsh.exe')

这篇关于通过python终端运行外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆