Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用函数python文件_Python_File_Function_File Io - Fatal编程技术网

调用函数python文件

调用函数python文件,python,file,function,file-io,Python,File,Function,File Io,我试图在从目录中打开某些文件后调用函数来解析文件。我需要在函数中打开文件吗?使用if语句,但在调用函数时不使用。Python新手无法使其工作。谢谢,在其他问题中找不到答案 #!usr/bin/env/ python import sys, re, os #function to find the packetloss data in pcoip server files def function_pcoip_packetloss(filename): lineContains = re

我试图在从目录中打开某些文件后调用函数来解析文件。我需要在函数中打开文件吗?使用
if
语句,但在调用函数时不使用。Python新手无法使其工作。谢谢,在其他问题中找不到答案

#!usr/bin/env/ python
import sys, re, os

#function to find the packetloss data in pcoip server files
def function_pcoip_packetloss(filename):
    lineContains = re.compile('.*Loss=.*')  #look for "Loss=" in the file
    for line in filename:
        if lineContains.match(line):    #check if line matches "Loss="
            print 'The file has: '  #prints if "Loss=" is found
            print line
            return 0;

#function to find the decrease in pcoip_server files
def function_pcoip_decrease(filename):
    lineContainsDecrease = re.compile('.*Decrease.*')
    for line in filename:
        if lineContainsDecrease.match(line):    #check if line matches "Decrease"
            print 'The file has: '          #prints if "Decrease is found"
            print line
            return 0;

for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
    lineContainsServerFile = re.compile('.*server.*')

    for filename in files:
        if lineContainsServerFile.match(filename):
            filename = os.path.join(root,filename)
            with open(filename,'rb') as files:
                #lineContainsLoss = re.compile('.*Loss=.*')
                filename = os.path.join(root,filename)
                for line in files:
                    function_pcoip_packetloss(files);
                    function_pcoip_decrease(files);
              #works with these if but when I call the function does not work
                #for line in files:
                    #if lineContainsLoss.match(line):
                        #print line

您混合了文件名和文件句柄的概念。在文件循环中使用文件不是一个好的做法。试试这个:

    for filename in files:
            if lineContainsServerFile.match(filename):
                    filename = os.path.join(root,filename)
                    with open(filename,'rb') as filehandle:
                         function_pcoip_packetloss(filehandle)
                         filehandle.seek(0)
                         function_pcoip_decrease(filehandle)

你到底有什么问题?错误消息?然后发布(包括完整的回溯)。您没有得到错误,但结果不是您期望的结果?告诉我们你得到了什么,你期望得到什么。帮助我们帮助您。@kindall no error msg,它在调用函数时似乎不起作用,但在代码中使用if语句作为注释时效果良好。我想要的是打开目录中的特定文件,在循环中打开大量文件,对其进行解析,并打印调用函数的匹配单词/行。如果你需要更多的细节,请告诉我。谢谢,“它似乎不起作用”正是我所说的那种事-(这实际上仍然不起作用,因为被调用的每个函数都通过
文件句柄
进行迭代。因此
函数
将获得耗尽的
文件句柄
迭代器,而实际上不做任何工作。是的-我忘记了只有第一部分可以工作-尝试组合这两个函数并进行检查每行谢谢,现在就可以了。新的bie抱歉只是一个简单的错误。你能简要解释一下它的文件名和文件句柄,以及什么是解决它的好办法,而不是在循环中使用文件?@Gkuner thank youfilename是文件的实际名称文件句柄是指向打开的文件对象的指针-还要注意回放广告ded to the codewait我如您所述尝试过,在解析完文件后似乎会拉出匹配的单词/行。第一个函数有效,但第二个函数无效,组合两个函数只起作用?或者我可以用其他方法吗?