0

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

2
  • Where are you telling it to sort by the datetime? Commented Feb 12, 2016 at 13:54
  • I added a possible error in the parse function as well as pointing to the error in the if. Commented Feb 12, 2016 at 14:19

2 Answers 2

1

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 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):
Sign up to request clarification or add additional context in comments.

Comments

0

Besides what sabbahillel said, test returns the correct listing, however this parse function looks weird, since you ask it to print the first item in the list, then the last, so it won't print all the files as you wish. The following code will print it correctly, although I believe your parse function is the root of your confusion:

import os

iostatslocalpath="/root/svc/testing/"
svchost='SVC_Cluster01'
nodenames=sorted(['75AKMX0', '75AKPD0', '172550', '172561'])

filelist=sorted(os.listdir(iostatslocalpath+svchost+'/.'))
# print filelist
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 in file and "Nv" in file:
                        test.append(file)
        for x in test:
            print(x)

Outputs:

Nv_stats_172550_160211_230030
Nv_stats_172550_160212_142624
Nv_stats_75AKPD0_160211_230030
Nv_stats_75AKPD0_160212_142624

So x are the files in the order you wish, then you parse them as you wish.

2 Comments

Thank you, it turns out I should have been using print filename[length-1] instead of print test[length-1] along with the modified and statement
Also this would only work for lists of len 2, since you're not printing every element, but only first and last. And filename[length-1] is not necessary, filename[-1] works fine in python. Lastly, could you accept my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.