0

Essentially what I want this code to do, is replace (systemType).clear() with either unix.clear or windows.clear. Is this even possible?

if(platform.system()=="Windows"):
    systemType="windows"
else:
    systemType="unix"

class unix:
    def clear():
        os.system('clear')

class windows:
    def clear():
        os.system('cls')


print("Detecing systemType")
time.sleep(1)
(systemType).clear()

The error is AttributeError: 'str' object has no attribute 'clear'

1
  • os.system('cls') if 'windows' in platform.system().lower() else os.system('clear') Commented Jul 17, 2015 at 19:08

2 Answers 2

6

If you move the assignment of systemType to after the class definitions, you can assign it the classes themselves rather than their names:

class unix:
    def clear():
        os.system('clear')

class windows:
    def clear():
        os.system('cls')

if platform.system()=="Windows":
    systemType = windows
else:
    systemType = unix

print("Detecing systemType")
time.sleep(1)
systemType.clear()
Sign up to request clarification or add additional context in comments.

Comments

0
import os
if os.name == "nt":
     os.system("cls") #windows
else:
     os.system("clear") #unix

1 Comment

This works, however I wanted an easy way to add functions to execute based on the operating system. Not just a cluster of if statements in one function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.