Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Refinement

Please provide amore in depth explanation on what os.name, os.system('cls'), and os.system('clear') is.

Please provide amore in depth explanation on what os.name, os.system('cls'), and os.system('clear') is.

1 Answer

andren
andren
28,558 Points

The os module is a module that exists to enable you to find info about the system the code is running on, as well as to execute non-python code on the system.

The os.name property tells you the name of the OS that the code is running on, nt is the family name of Windows operating systems, so if os.name is "nt" then that means your code is running on a Windows OS.

The os.system method allows you to run terminal commands on the system you are running on. So os.system('cls') tells Python to send the command cls to the terminal it is running in. cls is the command to clear the terminal of text on Windows. clear is the Unix equivalent command.

So essentially what the code does is check if the code is running on Windows, then if that is the case it sends the command cls to the terminal which clears it of text. If the OS is anything besides Windows then it sends the command clear, which does the same thing but works on Unix-like OSs which includes Mac OS, BSD and Linux.

If you simply typed os.system('cls') then the text clearing would only work on Windows, and os.system('clear') would only work on Unix-likes, which is why that if statement is necessary.