“如何修复”;尾部:写入错误:管道断裂“;在Python中运行linux命令?

“如何修复”;尾部:写入错误:管道断裂“;在Python中运行linux命令?,python,bash,Python,Bash,我试图逐行打印列表中的每个文件。 在文件的每一行末尾,它需要检查术语“.sh”是否在其中 我发现了错误 尾部:写入错误:“断管” 预期结果: 从列表中逐一阅读 如果文件行末尾有“.sh”一词,请检查文件的每一行 如果找到“.sh”,则打印 这就是我的atm机: # Modules import os from pprint import pprint # Files in list dirlist = ['test.txt','test2.txt','test3.txt'] # Loop t

我试图逐行打印列表中的每个文件。 在文件的每一行末尾,它需要检查术语“.sh”是否在其中

我发现了错误

尾部:写入错误:“断管”

预期结果:

  • 从列表中逐一阅读
  • 如果文件行末尾有“.sh”一词,请检查文件的每一行
  • 如果找到“.sh”,则打印
  • 这就是我的atm机:

    # Modules
    import os
    from pprint import pprint
    
    # Files in list
    dirlist = ['test.txt','test2.txt','test3.txt']
    
    # Loop to read the file in list
    for x in range (len(dirlist)):
        print ("Output of Filename: " + dirlist[x]
    
    # Variable to save the last 3 characters of the line
    last3 = os.popen ("cat " + dirlist[x] + " | tail -c 3")
    print last3
    
    # Read file
    f = open(dirlist[x], "r")
    # Loop to check if the keyword is the same as last3
    for l in f:
        if last3 in l:
            print ("FOUND IT!")
    
        else:
            print ("NOT IN IT!")
    
    结果:

    @Nic

    [![enter image description here][3][3]

    返回文件对象,而不是字符串

    请参阅:

    tail
    (实际上是stdio)在尝试写入输出但周围没有人读取时给出“断管”错误。(更具体地说,当它接收到
    SIGPIPE
    时)

    如果要使用
    popen
    启动子进程,则需要在程序退出之前完成管道中的读取

    在您的情况下,您可能应该使用
    subprocess.run
    而不是裸
    os.popen


    或者,更好的做法是,不要使用子流程进行简单的文件操作!只需在本机Python代码中执行这些操作,就会简单得多。

    我建议您在环境中使用本机Python代码,而不要使用open和os.popen

    这里有一个例子

    # Files in list
    dirlist = ['test.txt','test2.txt','test3.txt']
    
    # Loop to read the file in list
    for x in dirlist:
        print ("Output of Filename: " + x)
        with open(x) as f
            lines=f.readlines()
            for line in lines: #here you print each line
                print (line)
    
            if '.sh' in lines[-1:]: #check if .sh is in the last line
                print("found it")
            else:
                print("didnt find it")
    

    在@Nic Wanavit和Daniel Pyrden的帮助下,我终于修复了它

    我已将if/else放在循环中,否则它将检查所有行中的.sh而不是每行

    我在“.sh”部分中插入了括号,这很有效

    然而,我没有在最后3个字符中这样做,因为-1:由于某种原因对我不起作用

    # Files in List
    dirlist = ['test.txt', 'test2.txt', 'test3.txt']
    
    # Loop to read the file in list
    for x in dirlist:
     print ("Output of filename: "+  x)
    
     with open(x) as f:
      lines = f.readlines()
      for line lines:
       print ("Line in file: " + line)
    
       if (".sh" in line):
        print ("FOUND IT")
    
       else:
        print ("not found it \n")
    
    结果

    为什么要使用
    cat
    tail
    而不是打开文件并读取最后三行?我对Linux没有经验。这就是我所知道的。那么为什么要使用Linux解决Python问题呢?从文件中读取行完全可以用Python代码完成,不需要运行ny其他命令。问题是,除了Linux之外,我还必须用Python来做这件事。这只是为我想上的大学做准备。啊,我明白了。你的代码比我的代码容易得多,但它在[-1:]行上给出了一个I/O操作错误。你使用的是哪种Python版本?现在是2.7.16(但我已经安装了3.6版)。你能解释一下[-1:]的功能吗?@Nic:不要调用
    readlines()
    两次,只调用一次。一旦你读了文件中的所有行,下次就没有什么可读的了。(另外,如果文件很大,您可能希望对文件对象本身使用
    itertools.islice
    ,而不是使用
    readlines()
    将所有行读取到内存中。但这在这里可能不是问题。)@DanielPryden您是对的。我已相应地修改了答案。