简体   繁体   English

通过python终端运行外部程序

[英]Running external program through python terminal

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

>>>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

I guess '1' means that the command didnt executed succesfully?? 我猜“ 1”表示命令未成功执行? And when i run this in dos-terminal it creates the out.txt file with the result.But here not.. 当我在dos-terminal中运行它时,它会创建带有结果的out.txt文件。但是这里没有。

Is in.txt in the same directory that you are running your python script from? in.txt是否在您运行python脚本的目录中? You may be misinterpreting where the current working directory is from your function call. 您可能会从函数调用中误解当前工作目录的位置。 If not, instead of in.txt give a more specific path. 如果不是,请提供in.txt而不是更具体的路径。

Also, there is a subprocess module for external executable calls in python. 此外,还有一个subprocess模块,用于python中的外部可执行调用。

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')

You can add an r before the string 您可以在字符串前添加r

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

or use doubled backslases 或使用加倍的反光

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

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

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