Reputation: 379
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
Upvotes: 0
Views: 298
Reputation: 4425
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):
Upvotes: 1
Reputation: 1235
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.
Upvotes: 0