hi all..........

1)~ # free
cat /proc/meminfo
total used free shared buffers
Mem: 62192 32080 30112 0 0
Swap: 0 0 0
Total: 62192 32080 30112

2)~ # free
cat /proc/meminfo
total used free shared buffers
Mem: 62192 32440 29752 0 0
Swap: 0 0 0
Total: 62192 32440 29752
i want to caluculate difference between two as mentioned above ,i m not getting how to caluculate.............so plz help me.......

Recommended Answers

All 9 Replies

what exactly do you want to calculate?

can you be more specific?
:)

Something like this may give you a hint ...

def extract_number(data_str):
    """
    extract the numeric value from a string
    (the string should contain only one numeric value)
    return the number or None
    """
    s = ""
    for c in data_str:
        if c in '1234567890.-':
            s += c
    if s:
        # convert number to int or float
        return eval(s)
    else:
        return None

data = """\
cat /proc/meminfo
total used free shared buffers
Mem: 62192 32440 29752 0 0
Swap: 0 0 0
Total: 62192 32440 29752
"""

# create a list of the string data
data_list = data.split('\n')
print(data_list)  # test

print('-'*60)

# extract desired numeric values from the data in the list
for data_str in data_list:
    if data_str.startswith("Total:"):
        print(extract_number(data_str))

"""
621923244029752
"""

thanks for a reply......


i want to caluculate difference between 3 rd column[i.e, free ] ....for this calculation i want to select the 3 rd column from both files first ,so how to do this

Probably you want to use split() function:

sample = "Mem: 62192 32440 29752 0 0"
print sample.split()

Note that this splits on every white space, so you get the 3rd column by asking for the appropriate index which for this sample would be 3 ("Mem:" is the zero-th column)

thanks for a reply..........


i want to select only free and its value...

If it is always in the same column, then you do it as I suggested (but maybe it is not index 3) If it is not always in the same column, then split the header line, look for the one that is 'free' and use that same index in the subsequent lines.

k thanks for a reply

Things make more sense now.

If you just want to get the free memory, than you can read the "total line" to get the info. It is the last item in that line. Here is a modification of the original code ...

def get_free(data):
    # create a list of the string data
    data_list = data.split('\n')
    for data_str in data_list:
        if data_str.startswith("Total:"):
            #print(data_str)  # test
            #print(data_str.split())  # test
            return int(data_str.split()[3])
    

# test_data read as a string from file
data1 = """\
cat /proc/meminfo
total used free shared buffers
Mem: 62192 32080 30112 0 0
Swap: 0 0 0
Total: 62192 32080 30112
"""

free1 = get_free(data1)
print(free1)  # 30112

thanks vegaseat it s working good.............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.