File tree Expand file tree Collapse file tree 4 files changed +42
-0
lines changed Expand file tree Collapse file tree 4 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ # Battery Information of a Device.  
2+ The following script provides necessary battery information.
3+ 
4+ ## Running Script  
5+   *  Install dependencies ` pip install -r requirements.txt ` 
6+   *  Execute script ` python info.py ` 
7+ 
8+ ## Output  
9+ 
10+ ![ output] ( output.png ) 
Original file line number Diff line number Diff line change 1+ import  sys 
2+ import  psutil 
3+ 
4+ # Helper method to convert seconds to hours. 
5+ def  secs_to_hours (secs ):
6+     mm , ss  =  secs // 60 , secs % 60 
7+     hh , mm  =  mm // 60 , mm % 60 
8+     return  "%d:%02d:%02d"  %  (hh , mm , ss )
9+ 
10+ # Check if the OS is supported or not. 
11+ if  not  hasattr (psutil , "sensors_battery" ):
12+     sys .exit ("platform not supported" )
13+ 
14+ # Create battery instance. 
15+ batt  =  psutil .sensors_battery ()
16+ 
17+ # Check if battery is installed or not. 
18+ if  batt  is  None :
19+     sys .exit ("no battery is installed" )
20+ 
21+ # Print the current battery charge. 
22+ print (f"charge:     { round (batt .percent , 2 )}  " )
23+ 
24+ # Check if battery is plugged in or not. 
25+ if  batt .power_plugged :
26+     print (f"status:     { 'charging'  if  batt .percent  <  100  else  'fully charged' }  " )
27+     print ("plugged in: yes" )
28+ else :
29+     print (f"left:       { secs_to_hours (batt .secsleft )}  " )
30+     print (f"status:     discharging" )
31+     print (f"plugged in: no" )
Original file line number Diff line number Diff line change 1+ psutil 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments