繁体   English   中英

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

[英]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.

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