Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate percentage from variables

Status
Not open for further replies.

outspoken

IS-IT--Management
Jul 26, 2006
3
US
This is a pretty basic script, just wanting to grab some values from commandline and insert them into variables take those and calculate percentage.

import os
files = os.system('ls ../files/ | wc -l')
matches = os.system('wc -l ../text/burn.roto_02 | awk \'{print $1}\'')
percent=(files/matches)*100
print percent

I'll also need to tack a '.0' at the end of those variables so it returns a true value. example:

percent=(files.0/matches.0)*100

Running the below I get my expected value, but it is the variable which hangs things up for me.

python -c "percent=(20.0/100.0)*100;print percent"
20.0

Thanks!
 
that does work, thanks.

i'm having another problem now, trying to get the right values from my os.system commands. they just return the 0 value instead of the actual numbers from the shell script. i'm looking at 'popen' and 'argv' to see if these will help in grabbing the stdout from those commands.
 
ok, it is working now..

Code:
#!/usr/bin/python
import os
files = float(os.popen('ls ../files/ | wc -l').read().split()[0])
matches = float(os.popen('wc -l ../text/burn.roto_02').read().split()[0])
percent_matches=float(1.0*files/matches)*100
print percent_matches
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top