r/learnpython • • Oct 04 '26

[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

18 comments sorted by

View all comments

Show parent comment

1

u/__peg_parser__ Oct 22 '26

I RTFM'ed it. Docs:

os.system(command)
Execute the command (a string) in a subshell. [...]
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is recommended to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

It isn't marked as deprecated, but it recommends migrating to subprocess.

The "helpful recipes":

Replacing os.system()

sts = os.system("mycmd" + " myarg")
# becomes
retcode = call("mycmd" + " myarg", shell=True)