描述:之前檢視樹莓派的溫度一直都需要輸指令: cd /sys/class/thermal/thermal_zone0 然後cat temp 才能給出資料比如45084,給出的資料需要除以1000才是實際溫度值,路徑太長很不友善,是以想寫個腳本運作後自動給出結果。
網上已經有相關的文章,作為小白自然是拿來先跑一跑試試
參考文章 http://shumeipai.nxez.com/2014/10/04/get-raspberry-the-current-status-and-data.html
http://blog.csdn.net/xukai871105/article/details/38349209
網絡源碼如下:
import
os
# Return CPU temperature as a character string
def
getCPUtemperature():
res
=
os.popen(
'vcgencmd measure_temp'
).readline()
return
(res.replace(
"temp="
,"
").replace("
'C\n
","
"))
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def
getRAMinfo():
p
=
os.popen(
'free'
)
i
=
while
1
:
i
=
i
+
1
line
=
p.readline()
if
i
=
=
2
:
return
(line.split()[
1
:
4
])
# Return % of CPU used by user as a character string
def
getCPUuse():
return
(
str
(os.popen(
"top -n1 | awk '/Cpu\(s\):/ {print $2}'"
).readline().strip()))
# Return information about disk space as a list (unit included)
# Index 0: total disk space
# Index 1: used disk space
# Index 2: remaining disk space
# Index 3: percentage of disk used
def
getDiskSpace():
p
=
os.popen(
"df -h /"
)
i
=
while
1
:
i
=
i
+
1
line
=
p.readline()
if
i
=
=
2
:
return
(line.split()[
1
:
5
])
# CPU informatiom
CPU_temp
=
getCPUtemperature()
CPU_usage
=
getCPUuse()
# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats
=
getRAMinfo()
RAM_total
=
round
(
int
(RAM_stats[
])
/
1000
,
1
)
RAM_used
=
round
(
int
(RAM_stats[
1
])
/
1000
,
1
)
RAM_free
=
round
(
int
(RAM_stats[
2
])
/
1000
,
1
)
# Disk information
DISK_stats
=
getDiskSpace()
DISK_total
=
DISK_stats[
]
DISK_used
=
DISK_stats[
1
]
DISK_perc
=
DISK_stats[
3
]
if
__name__
=
=
'__main__'
:
print
('')
print
(
'CPU Temperature = '
+
CPU_temp)
print
(
'CPU Use = '
+
CPU_usage)
print
('')
print
(
'RAM Total = '
+
str
(RAM_total)
+
' MB'
)
print
(
'RAM Used = '
+
str
(RAM_used)
+
' MB'
)
print
(
'RAM Free = '
+
str
(RAM_free)
+
' MB'
)
print
('')
print
(
'DISK Total Space = '
+
str
(DISK_total)
+
'B'
)
print
(
'DISK Used Space = '
+
str
(DISK_used)
+
'B'
)
print
(
'DISK Used Percentage = '
+
str
(DISK_perc))
運作結果:提示:CPU Temperature = VCHI initialization failed初始化失敗 ,至于為什麼失敗,
我猜測是該作者的樹莓派和我的派在配置上有些不同
,我的解決思路是:既然不能自動調出溫度,不如把temp檔案的絕對路徑寫上,讀取temp檔案裡的資料即可。
以下是幾次錯誤嘗試: 1 def getCPUtemperature():
res = os.popen('/sys/class/thermal/thermal_zone0').readline()
return(res.replace("temp=","").replace("'C\n","")) 2 def getCPUtemperature():
res = os.popen(' cd sys/class/thermal/thermal_zone0/temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
提示沒有權限
3
def getCPUtemperature():
res = os.popen(' sudo cd sys/class/thermal/thermal_zone0/temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
執行結果顯示的溫度是五位整數,顯然除以1000才合适
剛開始試圖直接用res除以1000,若幹次失敗嘗試之後發現:提示真的是個好東西
系統早就提示了,字元串不能進行算術運算.....
接着:我詳細了解了
def函數 (自定義函數)
return()(每個函數都必須有的傳回值)
res (清單)
os.popen 格式: os.popen(command[, mode[, bufsize]])括号内是執行的一段程式,程式的值傳回給變量
詳情連結 http://www.runoob.com/python/os-popen.html
readline() read() readlines() 的差別 ,在這裡os.popen()括号内的程式實作的功能是讀取temp檔案的内容并轉換成
string格式(字元串格式),replace()在這裡的功能是将所讀取内容中的逗号和空格删除(thermal_zone0路徑下的temp
中隻有一個五位數,用不着去空格)
另外,read()readline()readlines()是檔案的屬性,是用來讀寫檔案内容的
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zdNlXR610MBpXT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zN1MjMxgzMxATOyMDM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
print('CPU Temperature = '+CPU_temp) 在列印結束階段,print()中的+号起到了字元串連接配接的功能,
因為temp中的數字是以字元串的形式儲存的,不能直接進行除以1000這樣的運算,必須将字元串轉換成浮點型再做運算
運算完成後還需要将浮點型的結果再轉換成字元串,配合列印階段的字元串連接配接。也就形成了下面的這段程式:
def getCPUtemperature():
res = os.popen('sudo cat /sys/class/thermal/thermal_zone0/temp').readline()
tempfloat=float(res) / 1000
temp=str(tempfloat)
return(temp)
修改完後運作成功。
附:
python中的字元數字之間的轉換函數
http://www.cnblogs.com/wuxiangli/p/6046800.html