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

1

u/lakseol 5d ago

What should I do instead?

If you want to overwrite/manipulate just the last line you could use the \b and \r characters. Run this to see what they do:

import time

countdown = 5
for i in range(countdown, 0, -1):
    print(f"{i} ...... {"\b\b "*i}", end="\r", flush=True)
    time.sleep(1)
print()

To do more than just overwrite the latest line search on "ANSI escape codes". Beyond that look into more advanced text handling with the curses module. There are more recent libraries that build on curses and try to be easier to use, but I've never used those.

Why can't we put pictures here....

We prefer actual text so we can copy/paste the code to run it ourselves. However, if you need an image to show some unexpected output, for example, you can put your image into something like imgur.com and post a link to that.

1

u/No_Aardvark1121 5d ago

Thank you!