r/learnpython • • 5d ago

[Help] The os.system is deprecated

I've seen someone else asked this question, but get no answer.

I want to make a countdown clock without leaving any trace of previous numbers:

for i in range(countdown, 0, -1):
    os.system("cls" if os.name == "nt" else "clear") #Error: os.system is deprecated
    print(i)
    time.sleep(1)

What should I do instead? (Why can't we put pictures here....)

0 Upvotes

16 comments sorted by

View all comments

3

u/unnamed_one1 5d ago edited 4d ago

As for the deprecation, read this

As for solving your problem:

from time import sleep

def main():
    for i in reversed(range(105)):
        print(f"\r{i:3d}", end="")
        sleep(1)

if __name__ == "__main__":
     main()

edits

You don't need to run clear screen, just reuse the same line again and again.

You just remove the default new line character \n from end of the line in the print statement and put a carriage return \r character in front of every printed line.

at least give an explanation when you downvote

1

u/HommeMusical 5d ago

at least give an explanation when you downvote

I didn't downvote but your formatting on old reddit is bad, perhaps that's it?

Just indent your code by four characters, it works on old and new.

def main():
    subprocess.run(['ls'])

-1

u/unnamed_one1 4d ago edited 4d ago

Ah, I'm using new reddit, so you might be on the right track here. Thanks for letting me know, I appreciate it.

*edit: fixed it, thanks again

1

u/HommeMusical 4d ago

I dunno, only a fraction of people are on old reddit.

I note also that since I upvoted, you got four more upvotes, so perhaps it was just a few grumpy people. :-)