Resetting Python list contents in nested loop
Question: I have some files in this format that I need to return the oldest and newest
files to pass to a function for parsing

    
    
    Nv_NODE_DATE_TIME
    

I would like the output to be

    
    
    Nv_stats_172550_160211_230030
    Nv_stats_172550_160212_142624
    Nv_stats_75AKPD0_160211_230030
    Nv_stats_75AKPD0_160212_142624
    

but I am getting the absolute first item and absolute last item

    
    
    Nv_stats_172550_160211_230030
    Nv_stats_75AKPD0_160212_142624
    Nv_stats_172550_160211_230030
    Nv_stats_75AKPD0_160212_142624
    

Here is the current code

    
    
    import os
    
    iostatslocalpath="/root/svc/testing/"
    svchost='SVC_Cluster01'
    nodenames=['75AKMX0', '75AKPD0', '172550', '172561']
    
    filelist=sorted(os.listdir(iostatslocalpath+svchost+'/.'))
    totalfilenumber=len(filelist)
    
    def parse(filename, length):
            print filename[0]
            print test[length-1]
    
    for nodename in nodenames:
            test=[]
            test[:]=[]
            for file in filelist:
                    if nodename and "Nv" in file:
                            test.append(file)
            parse(test, len(test))
    

There is probably something small I am overlooking, any help would be
appreciated


Answer: Note that the

    
    
    def parse(filename, length):
        print filename[0]
        print test[length-1]
    

uses test. You should probably make it

    
    
    def parse(filename, length):
        print filename[0]
        print filename[length-1]
    

Then

    
    
    if nodename and "Nv" in file:
    

does the in first and then does the and. [5.15. Operator
precedence](https://docs.python.org/2/reference/expressions.html) It thus is
the equivalent of

    
    
    if (nodename) and ("NV" in file):
    

Since you are looping over nodename the first section is alway true.

You probably want to use

    
    
    if (nodename in file) and ("Nv" in file):
    

