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,364 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.