Pythons platform.system() gives me str object has no attribute system but only in script
Question:
If I do like this in the python prompt:
import platform
platform.system()
I get Linux as expected.
However if I do like this in my script:
import platform
if(platform.system() == "windows"):
print x
else:
print y
I just get this error messsage.
AttributeError: str object has no attribute system
I am quite new to Python but this puzzles me a bit so if anyone can point to the problem I would be grateful.
Answers:
Somewhere in your script you have a variable called platform
that shadows the module with the same name.
your if statement has unneeded parenthesis. Im assuming you’re used to using a language that needs the condition to be wrapped in them.
Your code:
import platform
if(platform.system() == "windows"):
print x
else:
print y
Fixed code:
import platform
if platform.system() == "windows":
print x
else:
print y
If I do like this in the python prompt:
import platform
platform.system()
I get Linux as expected.
However if I do like this in my script:
import platform
if(platform.system() == "windows"):
print x
else:
print y
I just get this error messsage.
AttributeError: str object has no attribute system
I am quite new to Python but this puzzles me a bit so if anyone can point to the problem I would be grateful.
Somewhere in your script you have a variable called platform
that shadows the module with the same name.
your if statement has unneeded parenthesis. Im assuming you’re used to using a language that needs the condition to be wrapped in them.
Your code:
import platform
if(platform.system() == "windows"):
print x
else:
print y
Fixed code:
import platform
if platform.system() == "windows":
print x
else:
print y