This description may be a bit complicated so I will try to keep it short.
I have the following code that is working correctly...
def singlelist():
from datetime import datetime
from subprocess import Popen
from subprocess import PIPE
output=Popen(["sar","-r"], stdout=PIPE).communicate()[0]
date=datetime.now()
date=str(date).split()[0]
listtimeval=[]
for line in output.split('\n'):
if line == '' or 'Average' in line or 'kb' in line or 'Linux' in line or 'RESTART' in line:
pass
else:
(time,ampm,field1,field2,field3,field4,field5,field6,field7) = line.split()
listtimeval.append((time + " "+ ampm + "," + field3).split(','))
updatelist= [ [str(date) + " " +x[0],x[1]] for x in listtimeval]
return updatelist
val=singlelist()
...notice how time,ampm,etc are not defined previously...
I am trying to make this more dynamic as the output of sar will not always have the same number of columns.
What I want to do is this...
def fields(method):
if method == '-r':
nf = (time,ampm,field1,field2,field3,field4,field5,field6,field7)
return nf
def singlelist(nf):
from datetime import datetime
from subprocess import Popen
from subprocess import PIPE
output=Popen(["sar","-r"], stdout=PIPE).communicate()[0]
date=datetime.now()
date=str(date).split()[0]
listtimeval=[]
for line in output.split('\n'):
if line == '' or 'Average' in line or 'kb' in line or 'Linux' in line or 'RESTART' in line:
pass
else:
nf = line.split()
listtimeval.append((time + " "+ ampm + "," + field3).split(','))
updatelist= [ [str(date) + " " +x[0],x[1]] for x in listtimeval]
return updatelist
method='-r'
nf=fields(method)
val=singlelist(nf)
However I am getting this...
Traceback (most recent call last):
File "./Logic.py", line 110, in <module>
nf=fields(method)
File "./Logic.py", line 58, in fields
nf = (time,ampm,field1,field2,field3,field4,field5,field6,field7)
NameError: global name 'time' is not defined
How can I accomplish this?