Building upon my comment: my bet here is the script works perfectly, but you expect to see output right away and, not seeing any, abort the program.
However, read() will stop your script until the command completes. And only then will the pringint happen. Thus, no output will ever be displayed until after the command has completed.
I would change it like this:
with os.popen('cleanmgr.exe /sagerun:1') as fd:
chunks = iter(lambda: fd.read(1), '')
for chunk in chunks:
sys.stdout.write(chunk)
The idea being to print as it goes: when a size is specified, read
does not loop until the descriptor is closed, it returns as soon as it reads something.
It's not the most efficient here as it will read chars one by one. For the programs you're running it should not matter much, and reading more without introducing buffering delays with python is complex.
If you are not using the output in your program and just want it to pass through, maybe you'd be better off just calling :
import subprocess
subprocess.check_call(['cleanmgr.exe', '/sagerun:1'])
With no additional parameters, output will just go to your script's output. The function waits until the command completes before returning.