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

Dee K
Dee K
17,815 Points

Questions about the clear() function.

def clear (): if os.name == 'nt': os.system('cls') else: os.system('clear')

In this function, I am still confused about what os.system('cls') does? I know that if my operating system is a windows (which it is) then the os.system('cls') occurs.

What exactly is the purpose of os.system('cls')?

Thanks

1 Answer

srikanth chavvakula
srikanth chavvakula
6,178 Points

Hello Kim,

The purpose of os.system() is to invoke any external program (all exe files that are generally available in system32 folder) that you can generally open via the "windows--> Run" dialog box. So, in simple terms the command uses the windows subshell (command prompt) to invoke an external program. For ex: try the command "os.system('notepad') which will actually invoke the notepad on windows, which is very similar to opening notepad via the command prompt. Also try os.system('Calc') which will open the calculator in windows.

By the way, "cls" is a command that is used to "CLEAR SCREEN" on the windows command prompt.

Hope this is helpful.

Dee K
Dee K
17,815 Points

Hey Srikanth,

Thanks for your in depth answer. That really helped a lot!