Getting processor information in Python

后端 未结 10 1997
一向
一向 2020-11-29 04:20

Using Python is there any way to find out the processor information... (I need the name)

I need the name of the processor that the interpreter is running on. I check

10条回答
  •  孤独总比滥情好
    2020-11-29 05:15

    For Linux, and backwards compatibility with Python (not everyone has cpuinfo), you can parse through /proc/cpuinfo directly. To get the processor speed, try:

    # Take any float trailing "MHz", some whitespace, and a colon.
    speeds = re.search("MHz\s*: (\d+\.?\d*)", cpuinfo_content)
    

    Note the necessary use of \s for whitespace.../proc/cpuinfo actually has tab characters and I toiled for hours working with sed until I came up with:

    sed -rn 's/cpu MHz[ \t]*: ([0-9]+\.?[0-9]*)/\1/gp' /proc/cpuinfo
    

    I lacked the \t and it drove me mad because I either matched the whole file or nothing.

    Try similar regular expressions for the other fields you need:

    # Take any string after the specified field name and colon.
    re.search("field name\s*: (.+)", cpuinfo_content)  
    

提交回复
热议问题