从python脚本执行命令行参数失败

2024-10-04 03:30:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过python脚本运行命令行参数。脚本触发.exe,但它抛出一个错误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\Anite\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"

Unhandled Exception: System.IO.IOException: The handle is invalid.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)

at ALV.Console.CommandLineParametersHandler.ConsoleWriteLine(String message, Boolean isError)

at ALV.Console.CommandLineParametersHandler.InvokeActions()

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

我使用的是Windows7和Python2.7.13。请建议克服这个问题。在

编辑: 我也尝试过按照下面的代码以列表s的形式传递命令,但问题仍然存在。在

^{pr2}$

Tags: cmdexecuteosprocessexesystemcommandat
1条回答
网友
1楼 · 发布于 2024-10-04 03:30:22

根据你的错误信息,我认为这个问题是ALVConsole.exe,而不是您的Python脚本。在

当你重定向输出时,ALVConsole.exe尝试对控制台执行某些操作(如设置光标位置,或获取终端的大小),但失败如下。在

有国旗吗ALVConsole.exe它将输出修改为机器可读版本?我找不到这个程序的文档。在

相关问题 更多 >