cmd closing when using the cls/clear command

Asked

Viewed 120 times

2

I made a function that identifies if the person is using windows or linux, and with that runs the right command to clean cmd/terminal, worked on the visual studio code console, however when I will run the file . py it closes...

Code:

import os
import platform

def limpar():
    if (platform.system() == 'Windows'):
        os.system('cls')
    if (platform.system() == 'Linux'):
        os.system('clear')
  • 2

    https://answall.com/q/72678/112052

  • 2

    Important [Edit] the post and provide a [mcve] of the problem. In the given code the function is never called, and if added, it works normally.

2 answers

1

I ran your code with some changes on Ubuntu 20.04 and everything went well.

import os
import platform


def limpar():
    if platform.system() == 'Windows':
        return os.system('cls')

    os.system('clear')

if __name__ == "__main__":
    limpar()
  • 1

    Apparently it is windows cmd problem, with this code cmd tb closed :/

1

I guess you forgot to call clear() at the end of the script! Mine worked just fine! So I did:

import os
import platform

def limpar():
       if (platform.system() == 'Windows'):
             os.system('cls')
       if (platform.system() == 'Linux'):
             os.system('clear')
limpar()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.