![](/img/trans.png)
[英]Execute a terminal command that takes an argument from terminal in python a 2.6 script
[英]Failed to execute command line argument from python script
我正在嘗試通過python腳本運行命令行參數。 腳本觸發.exe,但由於System.IO.IOException: The handle is invalid.
而引發錯誤System.IO.IOException: The handle is invalid.
。 以下是我的代碼:
import os , sys , os.path
from subprocess import call
import subprocess, shlex
def execute(cmd):
"""
Purpose : To execute a command and return exit status
"""
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(result, error) = process.communicate()
rc = process.wait()
if rc != 0:
print "Error: failed to execute command:",cmd
print error
return result
found_alf = r"C:\AniteSAS\ResultData\20170515\Run01\1733200515.alf"
filter_alvf = r"C:\Users\sshaique\Desktop\ALF\AniteLogFilter.alvf"
command = str(r'ALVConsole.exe -e -t -i ' + '\"'+found_alf+'\"' + ' --ffile ' + '\"'+filter_alvf+'\"')
print command
os.chdir('C:\Program Files\Anite\LogViewer\ALV2')
print os.getcwd()
print "This process detail: \n", execute(command)
輸出如下:
ALVConsole.exe -e -t -i“ C:\\ AniteSAS \\ ResultData \\ 20170515 \\ Run01 \\ 1733200515.alf” --ffile“ C:\\ Users \\ sshaique \\ Desktop \\ ALF \\ AniteLogFilter.alvf” C:\\ Program Files \\安耐特\\ LogViewer的\\ ALV2
該過程詳細信息:錯誤:無法執行命令:ALVConsole.exe -e -t -i“ C:\\ AniteSAS \\ ResultData \\ 20170515 \\ Run01 \\ 1733200515.alf” --ffile“ C:\\ Users \\ sshaique \\ Desktop \\ ALF \\ AniteLogFilter.alvf”
未處理的異常:System.IO.IOException:句柄無效。
在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)
在System.Console.GetBufferInfo處(布爾throwOnNoConsole,布爾和成功)
在ALV.Console.CommandLineParametersHandler.ConsoleWriteLine(字符串消息,布爾值isError)
在ALV.Console.CommandLineParametersHandler.InvokeActions()
在ALV.Console.Program.Main(String [] args)
當我從上面的輸出中復制命令行參數並從cmd手動運行時,它工作正常。 ALVConsole.exe -e -t -i "C:\\AniteSAS\\ResultData\\20170515\\Run01\\1733200515.alf" --ffile "C:\\Users\\sshaique\\Desktop\\ALF\\AniteLogFilter.alvf"
我正在使用Windows 7和Python 2.7.13。 請建議解決此問題。
編輯:我也試圖按照以下代碼將命令作為列表s
傳遞,但問題仍然相同。
command = str(r'ALVConsole.exe -e --csv -i ' + '\"'+found_alf+'\"' + ' --ffile ' + '\"'+filter_alvf+'\"')
s=shlex.split(command)
print s
print "This process detail: \n", execute(s)
根據您的錯誤消息,我認為此問題與ALVConsole.exe有關,而不是Python腳本。
當您重定向輸出時,ALVConsole.exe會嘗試對控制台執行某些操作(例如設置光標位置或獲取終端的大小),但這樣會失敗。
是否有ALVConsole.exe的標志將輸出修改為機器可讀的版本? 我找不到該程序的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.