首页 > 解决方案 > 我的 Python 代码适用于我的 MacBook Pro,但不适用于任何其他 MacBook

问题描述

每个人。我是一个刚接触 python 的学生,我刚刚写了一个脚本,它在我的 MacBook 上运行得很好,但是每当我尝试在其他人的 MacBook 上运行它时,它都会给我一个错误消息说

SyntaxError: invalid non-printable character U+00A0. 

即使我尝试在线搜索,我也不知道发生了什么,有人可以帮助我吗?

这是我的代码:

#!/usr/bin/env python3
import socket
import shutil
import psutil
import math
import MacTmp
import time
import urllib.request
import os

def check_disk_usage(disk):
    """Verifies that there's enough free space on disk"""
    du = shutil.disk_usage(disk)
    free = du.free / du.total * 100
    return free > 20
def check_cpu_usage():
    """Verifies that there's enough unused CPU"""
    usage = psutil.cpu_percent(1)
    return usage < 75
def disk_amount(disk):
    """The amount of free disk"""
    am = shutil.disk_usage(disk)
    free = am.free / am.total * 100
    return math.floor(free)
def cpu_amount():
    """The amount of cpu usage"""
    used = psutil.cpu_percent(1)
    return math.floor(used)
def check_localhost():
  localhost = socket.gethostbyname('localhost')
  return localhost == "127.0.0.1"

def internet_on():
        try:
            urllib.request.urlopen('http://www.google.com', timeout=2)
            return True
        except:
            return False

size = os.get_terminal_size()

while True:
    if not check_disk_usage('/'):
        print("No disk for use!")
    elif not check_cpu_usage():
        print("You are using too much CPU")
    elif not check_localhost():
        print("Not the LOCALHOST")
    elif internet_on() == False:
        print("No Internet Connection")
    elif check_localhost() and check_cpu_usage() and check_disk_usage('/'):
        print("Everything is OK")
        print("|Free Disk: " + str(disk_amount('/')) + "|" + "|CPU Usage: " + str(cpu_amount()) + "|" + "|CPU Temperature: " + str(MacTmp.CPU_Temp()) +"|Internet: " + str(internet_on()) + "|")
    time.sleep(5)
    if int(float(cpu_amount())) >= 75 or int(float(MacTmp.CPU_Temp())) >= 95:
        break
    elif int(size.lines > 70):
        os.system('cls||clear')
print("Too much CPU is using now or Your device is very hot!")

谢谢!

标签: python

解决方案


推荐阅读