简体   繁体   English

无法从python脚本执行命令行参数

[英]Failed to execute command line argument from python script

I am trying to run a command line argument through python script. 我正在尝试通过python脚本运行命令行参数。 Script triggers the .exe but it throws an error as System.IO.IOException: The handle is invalid. 脚本触发.exe,但由于System.IO.IOException: The handle is invalid.而引发错误System.IO.IOException: The handle is invalid. . Following is my code : 以下是我的代码:

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)

Output is as follows : 输出如下:

ALVConsole.exe -e -t -i "C:\\AniteSAS\\ResultData\\20170515\\Run01\\1733200515.alf" --ffile "C:\\Users\\sshaique\\Desktop\\ALF\\AniteLogFilter.alvf" C:\\Program Files\\Anite\\LogViewer\\ALV2 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

This process detail: Error: failed to execute command: ALVConsole.exe -e -t -i "C:\\AniteSAS\\ResultData\\20170515\\Run01\\1733200515.alf" --ffile "C:\\Users\\sshaique\\Desktop\\ALF\\AniteLogFilter.alvf" 该过程详细信息:错误:无法执行命令:ALVConsole.exe -e -t -i“ C:\\ AniteSAS \\​​ ResultData \\ 20170515 \\ Run01 \\ 1733200515.alf” --ffile“ C:\\ Users \\ sshaique \\ Desktop \\ ALF \\ AniteLogFilter.alvf”

Unhandled Exception: System.IO.IOException: The handle is invalid. 未处理的异常:System.IO.IOException:句柄无效。

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)

at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded) 在System.Console.GetBufferInfo处(布尔throwOnNoConsole,布尔和成功)

at ALV.Console.CommandLineParametersHandler.ConsoleWriteLine(String message, Boolean isError) 在ALV.Console.CommandLineParametersHandler.ConsoleWriteLine(字符串消息,布尔值isError)

at ALV.Console.CommandLineParametersHandler.InvokeActions() 在ALV.Console.CommandLineParametersHandler.InvokeActions()

at ALV.Console.Program.Main(String[] args) 在ALV.Console.Program.Main(String [] args)

When I copy the command line argument from the above output and run manually from cmd it works fine. 当我从上面的输出中复制命令行参数并从cmd手动运行时,它工作正常。 ALVConsole.exe -e -t -i "C:\\AniteSAS\\ResultData\\20170515\\Run01\\1733200515.alf" --ffile "C:\\Users\\sshaique\\Desktop\\ALF\\AniteLogFilter.alvf"

I am using Windows 7 and Python 2.7.13 for. 我正在使用Windows 7和Python 2.7.13。 Please suggest overcoming this issue. 请建议解决此问题。

EDIT: I have also tried to pass command as a list s as per below code but the issue remains the same. 编辑:我也试图按照以下代码将命令作为列表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)

Based on your error messages I think that this problem is with ALVConsole.exe, not your Python script. 根据您的错误消息,我认为此问题与ALVConsole.exe有关,而不是Python脚本。

When you redirect the output, ALVConsole.exe tries to do something to the console (like setting cursor position, or getting the size of the terminal) but fails like this. 当您重定向输出时,ALVConsole.exe会尝试对控制台执行某些操作(例如设置光标位置或获取终端的大小),但这样会失败。

Is there a flag to ALVConsole.exe that modifies the output to a machine-readable version? 是否有ALVConsole.exe的标志将输出修改为机器可读的版本? I wasn't able to find the documentation for this program. 我找不到该程序的文档。

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

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