Skip to content Skip to sidebar Skip to footer

Python Custom Delimiter For Read Or Readline

I am interacting with a subprocess and trying to detect when it is ready for my input. The problem that I am having is that the read or readline functions rely on the '\n' delimite

Solution 1:

You can implement your own readlines function and choose the delimiter yourself:

defcustom_readlines(handle, line_separator="\n", chunk_size=64):
    buf = ""# storage bufferwhilenot handle.closed:  # while our handle is open
        data = handle.read(chunk_size)  # read `chunk_size` sized data from the passed handleifnot data:  # no more data...break# break away...
        buf += data  # add the collected data to the internal bufferif line_separator in buf:  # we've encountered a separator
            chunks = buf.split(line_separator)
            buf = chunks.pop()  # keep the last entry in our bufferfor chunk in chunks:  # yield the restyield chunk + line_separator
    if buf:
        yield buf  # return the last buffer if any

Unfortunately, due to Python default buffering policies you won't be able to grab large swaths of data if they are not provided by the process you're calling, but you can always resort to setting the chunk_size to 1 and then read the input character by character. So, for your example, all you need to do is:

import subprocess

proc = subprocess.Popen(["your", "subprocess", "command"], stdout=subprocess.PIPE)

while chunk in custom_readlines(proc.stdout, ">", 1):
    print(chunk)
    # do whatever you want here...

And it should capture everything up to > from your subprocesses' STDOUT. You can also use multiple characters as separators in this version.

Post a Comment for "Python Custom Delimiter For Read Or Readline"