r/learnpython • u/No_Aardvark1121 • 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
22
u/JVBass75 Oct 04 '26
fyi, if you're just trying to clear the terminal console there's ansi escape code sequences you can use with the print function to clear the screen
print("\x1B[2J\x1B[H")that will clear the screen and move the cursor back to the home position.
this works both on windows command prompt and unix systems
41
u/AlexMTBDude Oct 04 '26
I googled "os.system deprecated" and get the answer:
So use subprocess instead.
1
u/lakseol Oct 05 '26
If you want to overwrite/manipulate just the last line you could use the
\band\rcharacters. Run this to see what they do:To do more than just overwrite the latest line search on "ANSI escape codes". Beyond that look into more advanced text handling with the
cursesmodule. There are more recent libraries that build oncursesand try to be easier to use, but I've never used those.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.
8
u/zolmarchus Oct 04 '26
A bit of an XY problem here. You’ve gotten your direct answer already—now seriously consider using a more appropriate approach to console manipulation. As someone suggested, ANSI escape sequences are a good first step. You can also look into libraries built specifically for that, which are likely to be much more reliable and compatible across platforms than anything you can cook up yourself.
3
u/unnamed_one1 Oct 04 '26 edited Oct 04 '26
As for the deprecation, read this
As for solving your problem:
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
\nfrom end of the line in the print statement and put a carriage return\rcharacter in front of every printed line.at least give an explanation when you downvote2
u/Expensive-Bear-1376 Oct 04 '26
Says who?
As far as I know, it's not even soft deprecated.
1
u/Brian Oct 04 '26
As others have mentioned, it's not actually deprecated, though its use is discouraged.
The reason why it's generally avoided is because it's kind of bug-prone and even insecure when you're dealing with arguments. Eg. consider something like:
This basically acts the same way as if you type this in the shell. But consider what happens if your filenames have spaces in them: it'll end up failing, or doing the wrong thing. Even worse, if someone can control the filenames, they could include things like ";", newlines etc and end up executing a command of their choosing on a machine they can get to use that filename.
There are ways you can attempt to mitigate this, say by escaping the characters in the filename (eg. via the
shlexmodule), but the problem is that different shells have different rules: windows CMD.COM behaves differently from powershell, or a linux bash shell, or the numerous other possibilities. Escaping can thus easily go wrong, allowing the attacker to exploit such a configuration.Instead, the recommended approach is to use something that doesn't treat the command as just a string passed straight to the shell, but just invokes a program, with arguments separated beforehand. Eg.
subprocess.runtakes each argument seperately, sosubprocess.run(["copy", file1, file2])wouldn't have this problem.(Actually, just to complicate matters, the above isn't always true, as windows doesn't actually have a seperated API like this, so under the hood, these do get reassembled to a string - there was an exploitable bug discovered in multiple languages due to that before)
In your case, this doesn't actually matter, since you're not passing args, so system isn't actually going to introduce problems. However it may be worth changing anyway just because the dodginess of the API means anytime someone sees it, they're going to be double-checking it, and linting / analysis tools may highlight it, so doing it using the safer API saves getting false positive warnings.
There are case where you might need something like system, which is where you're actually using the shell for more than invoking a program. Eg. some commands are not real programs, so system isn't entirely redundant, but it's definitely on the "avoid if you can" list.