0

hello before I say anything I would like to let you know that I tried searching for the answer but I found nothing.

whenever I use os.system('cls') it clears the screen but it prints out a zero.

is this normal, if not how do I stop it from doing that?

Reznik
  • 2,663
  • 1
  • 11
  • 31
blobodep
  • 119
  • 1
  • 7
  • 1
    `_ = os.system('cls')` and then you're explicitly discarding the exit status instead of letting the REPL implicitly print it. But in a script you don't need to do that at all, because you don't have a REPL printing return values in the first place. – Charles Duffy Jun 15 '20 at 16:49
  • I'm curious what you used for searching that someone else was able to find 2 solutions. – Richard Jessop Jun 15 '20 at 16:54
  • stack overflow i searched "why does os.system("cls") print 0" i found nothing – blobodep Jun 16 '20 at 18:29

2 Answers2

3

Because os.system() returns the return code of the process:

On Windows, the return value is that returned by the system shell after running command.

For successful commands, this is zero.

Since you're running within the interactive interpreter (as this wouldn't happen otherwise), it prints the function's return value, 0.

When you're running a standalone script, e.g. a clear_hello.py with

os.system('cls')
print('Hello!')

you wouldn't see the zero.

AKX
  • 152,115
  • 15
  • 115
  • 172
1

I guess you running in inside an interpreter

os.system will return:

a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero)

So it just print the value it got, the return value of the command cls in the command line, which is 0 cause the command run successfully

chash
  • 3,975
  • 13
  • 29
Reznik
  • 2,663
  • 1
  • 11
  • 31