AttributeError: 'function' object has no attribute 'strip'

AttributeError: 'function' object has no attribute 'strip'

我正在尝试从 platform.system 值中去除白色 space,以便我可以在某些 if/else 逻辑中进行比较。我收到以下错误。我错过了什么?

第 9 行,在 打印(os_name.strip()) AttributeError: 'function' 对象没有属性 'strip'

import platform

os_name = platform.system
os_name_strip = os_name.strip()

print('OS Name      :', os_name_strip)

if os_name == 'Windows':
    print('we have an OS match')
else:
    print('we do not have an OS match')

您已将实际函数 platform.system 分配给 os_name 变量。要执行函数并存储输出值,请确保包含括号:

os_name = platform.system()

试试这个;

import platform
os_name = platform.system()
os_name_strip = os_name.strip()

print('OS Name      :', os_name_strip)

if os_name == 'Windows':
    print('we have an OS match')
else:
    print('we do not have an OS match')

OS Name      : Windows
we have an OS match