473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

output of top on a linux box

Hello,
I need the cpu usage of a linux box, for which i capture the output of
"top" using "popen". However, i am facing problems during string
handling. The code snippet is:-

top = os.popen("top -n 1")
rd = top.read().split("\n")
splt = rd[2].split()
# cpu = splt[2][:-4] # why ain't this working ???
cpu = splt[2]
print "cpu usage: %s, %s" % (cpu, type(cpu))

the "print" statement prints, "cpu usage: 0.5%sy, <type 'str'>". i
tried my best to get rid of the "%sy" after the "0.5", but couldn't
succeed (even though the type of "cpu" is "string").

Also, one weird thing that was happening was that if i still "split"
the cpu string (removed the last 4 characters), the output of the next
print statement was wrong (specifically, the first 2 characters got
chipped off). This output is totally unexpected, and wrong...

Can anyone kindly help me in understanding what exactly is going on here.

Thanks a lot!
Apr 9 '07 #1
0 995

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fraser Gordon | last post by:
Hello, Hopefully someone can help me out with this issue... I have a python script that needs to run a shell command and be able to react to output from that command as it occurs (as opposed...
0
by: amg | last post by:
--------------2ABCD8BC51CA575D47B206C8 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit --------------2ABCD8BC51CA575D47B206C8 Content-Type: text/plain;...
4
by: Ernesto | last post by:
Hi everybody: I am developing a library using mingw (windows). I created my binary files (.dll), my library file (.a) and my def file (.def) using g++ -output-def=XXX -soname=XXX The problem...
20
by: SR | last post by:
Hi, I need to read the output from a system( ) function within a C program. The function however only returns the exit status. How can I read what system( ) sends to stdout ? (is there a simpler...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
4
by: mwebel | last post by:
Hi, i want to get the adress of a pointer in a string. I did so by outputting it to a ostringstream: ------------------------------ char* ptr; //generate adress in hex format...
9
by: cniharral | last post by:
Hi, I'm interested in printing out coloured lines of my application and I don't know what to use. Can anybody give me an idea?? Regards. Carlos Niharra López
16
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables...
2
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.