【问题标题】:Python, redirecting the stream of Popen to a python functionPython,将 Popen 的流重定向到 python 函数
【发布时间】:2010-10-13 15:31:21
【问题描述】:

我是 python 编程的新手。 我有这个问题:我有一个文本文件列表(压缩和未压缩),我需要: - 连接到服务器并打开它们 - 打开文件后,我需要把他的内容传递给我写的另一个python函数

def readLogs (fileName):
f = open (fileName, 'r')
inStream = f.read()
counter = 0
inStream = re.split('\n', inStream) # Create a 'list of lines'
out = ""              # Will contain the output
logInConst = ""       # log In Construction
curLine = ""          # Line that I am working on

for nextLine in inStream:
    logInConst += curLine  
    curLine = nextLine
    #   check if it is a start of a new log && check if the previous log is 'ready'
    if newLogRegExp.match(curLine) and logInConst != "":

        counter = counter + 1

        out = logInConst
        logInConst = ""
        yield out

yield logInConst + curLine

def checkFile (regExp, fileName):
    generatore = readLogs(fileName)
    listOfMatches=[]

    for i in generatore: #I'm now cycling through the logs
        # regExp must be a COMPILE regular expression
        if regExp.search(i):
            listOfMatches.append(i)
    return listOfMatches

为了详细说明这些文件中包含的信息。 该函数的目的是使用 3 行仅在 1 行中写入存储在这些文件中的日志......该函数在从我的本地计算机读取的文件上运行良好,但我无法弄清楚如何连接到远程服务器和创建这些单行日志而不将每个文件的内容存储到一个字符串中,然后使用该字符串...我用来连接远程机器的命令是:

connection_out = Popen(['ssh', retList[0], 'cd '+retList[2]+'; cat'+fileName], stdout=PIPE).communicate()[0]

retList[0] 和 retList[2] 是 user@remote 和我必须访问的文件夹名称

提前感谢大家!

更新:

我的问题是我必须先建立一个 ssh 连接:

pr1=Popen(['ssh', 'siatc@lgssp101', '*~/XYZ/AAAAA/log_archive/00/MSG_090308_162648.gz*' ], stdout=PIPE).communicate()[0]

我需要打开的所有文件都存储在一个列表中,fileList[],其中一部分是压缩的(.gz),一部分只是文本文件!我已经尝试了你在 bot 之前显示的所有程序,但没有任何效果......我想我必须修改 Popen 函数的第三个参数,但我不知道该怎么做!有没有人可以帮帮我???

【问题讨论】:

    标签: python stream subprocess yield popen


    【解决方案1】:

    您不必自己将流/文件拆分为行。只需迭代:

    for ln in f:
        # work on line in ln
    

    这对于文件(使用 open() 处理 file())和管道(使用 Popen)应该同样适用。使用popen对象的stdout属性访问连接到子进程stdout的管道

    示例

    from subprocess import Popen, PIPE
    pp = Popen('dir', shell=True, stdout=PIPE)
    
    for ln in pp.stdout:
        print '#',ln
    

    【讨论】:

    • 我真的不明白,我应该在 Popen(['ssh', retList[0], 'cd '+retList[2]+'; cat'+fileName ], stdout=PIPE).communicate()[0] 在函数 readLogs 最外部的 FOR ???
    • 使用 stdout 属性访问管道。沟通在这里无济于事。我已经用一个例子更新了我的答案。
    【解决方案2】:

    删除 InStream 并仅使用文件对象。

    这样你的代码就会变成:

    for nextLine in f.readlines():
        .
        .
        .
    

    Ber 说得对。

    为了澄清,文件对象的默认迭代行为是返回下一行。所以“for nextLine in f”会得到与“for nextLine in f.readlines()”相同的结果。

    详见文件对象文档:http://docs.python.org/library/stdtypes.html#bltin-file-objects

    【讨论】:

      【解决方案3】:

      如果你想通过 ssh 做某事,为什么不使用the Python SSH module

      【讨论】:

        【解决方案4】:

        试试这个页面,到目前为止我找到的关于 popen 的最佳信息....

        http://jimmyg.org/blog/2009/working-with-python-subprocess.html

        【讨论】:

          猜你喜欢
          • 2014-09-13
          • 1970-01-01
          • 2018-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-13
          • 2021-04-29
          • 2010-10-15
          相关资源
          最近更新 更多