👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation. This requires me to be able to see each line of output from the wrapped program as soon as it is output.
I figured that I"d just execute the program using subprocess.Popen
, use stdout=PIPE
, then read each line as it came in and act on it accordingly. However, when I ran the following code, the output appeared to be buffered somewhere, causing it to appear in two chunks, lines 1 through 332, then 333 through 439 (the last line of output)
from subprocess import Popen, PIPE, STDOUT
p = Popen("svnadmin verify /var/svn/repos/config", stdout = PIPE,
stderr = STDOUT, shell = True)
for line in p.stdout:
print line.replace("
", "")
After looking at the documentation on subprocess a little, I discovered the bufsize
parameter to Popen
, so I tried setting bufsize to 1 (buffer each line) and 0 (no buffer), but neither value seemed to change the way the lines were being delivered.
At this point I was starting to grasp for straws, so I wrote the following output loop:
while True:
try:
print p.stdout.next().replace("
", "")
except StopIteration:
break
but got the same result.
Is it possible to get "realtime" program output of a program executed using subprocess? Is there some other option in Python that is forward-compatible (not exec*
)?
👻 Read also: what is the best laptop for engineering students?
Getting realtime output using subprocess __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Getting realtime output using subprocess __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Getting realtime output using subprocess, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Getting realtime output using subprocess
- Deutsch Getting realtime output using subprocess
- Français Getting realtime output using subprocess
- Español Getting realtime output using subprocess
- Türk Getting realtime output using subprocess
- Русский Getting realtime output using subprocess
- Português Getting realtime output using subprocess
- Polski Getting realtime output using subprocess
- Nederlandse Getting realtime output using subprocess
- 中文 Getting realtime output using subprocess
- 한국어 Getting realtime output using subprocess
- 日本語 Getting realtime output using subprocess
- हिन्दी Getting realtime output using subprocess
Moscow | 2023-03-24
Maybe there are another answers? What Getting realtime output using subprocess exactly means?. I am just not quite sure it is the best method
Singapore | 2023-03-24
imp Python module is always a bit confusing 😭 Getting realtime output using subprocess is not the only problem I encountered. Checked yesterday, it works!
New York | 2023-03-24
Simply put and clear. Thank you for sharing. Getting realtime output using subprocess and other issues with StackOverflow was always my weak point 😁. Checked yesterday, it works!