错误:'str'对象没有属性'system'



看起来像os.system((错误。我不知道下一步该怎么做。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re, platform, os
def init():
        cmd = {
                'centos': 'yum -y install python-devel && pip install psutil > /dev/null',
                'ubuntu': 'apt-get -y install python-dev && pip install psutil > /dev/null'
        }
        os = platform.platform().lower()
        osname = ''.join(key for key in cmd if re.findall(key,os))
        try:
                import psutil
        except ImportError:
                try:
                        os.system(cmd[osname])
                        import psutil
                except Exception as error:
                        print("Error : {}".format(error))
def collect():
        cpu = psutil.cpu_count(logical=False)
        print('cpucount: {}'.format(cpu))
if __name__ == '__main__':
        init()

os.system()函数不接受字符串???? os.system('apt-get -y install python-dev && pip install psutil > /dev/null'),它在python shell中运行时运行。我不知道错得有多离谱。

[root@master python]# python test.py 
Error : 'str' object has no attribute 'system'

os = platform.platform().lower()是你的罪魁祸首。对该局部变量使用不同的名称。

相关内容

最新更新