簡體   English   中英

從Expect腳本調用python腳本時出錯

[英]Error when calling a python script from expect script

我有一個名為mypython.py的python腳本,其中導入了一個記錄器模塊。 從名為myexpect.exp的期望腳本中調用mypython.py。 我的問題是,當我在mypython.py中添加logger.info('test')語句並從myexpect.exp調用它時,myexpect.exp失敗。 如果我用print語句替換logger.log('test')語句,那么它工作正常。

python腳本的內容

import os
import sys
import datetime
import time
import argparse
import logging

logging.basicConfig(format="%(asctime)s %(message)s",
                datefmt='%m/%d/%Y %I:%M:%S %p',
                level=logging.INFO)
logger = logging.getLogger(__name__)

***
***
def main():
    args = get_args()

    print 'This is from print statement ' + args.test_arg ### This is OK
    logger.info("This is from logger statement" + " " + args.test_arg) ### This has issue

期望腳本的內容

#!/usr/bin/expect --

#exec python mypath/stratus/bin/mypython.py "test" ">@stdout"
exec python prnt_stmt.py -t arg_from_exp ">@stdout"

我得到的錯誤

This is from print statement arg_from_exp
03/06/2014 03:16:55 AM This is from logger statement arg_from_exp
    while executing
"exec python mypython.py -t arg_from_exp ">@stdout""
    (file "./myexpect.exp" line 9)

有人可以幫我嗎?

Tcl的exec命令有點奇怪。 如果它會拋出一個錯誤

  1. 執行命令返回非零退出狀態,或者
  2. 執行命令將寫入stderr。

請參閱http://wiki.tcl.tk/exec的“子狀態”部分

您可以這樣做:

set status [catch {exec -ignorestderr python mypython.py -t arg_from_exp} output]
if {$status == 0} {
    puts "no problem"
} else {
    puts "non-zero exit status"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM