Here, I add another loop around open and the read loop. Normally, opening the FIFO blocks until the other end is opened also. To do this efficiently, you can (and should) take advantage of the fact that Here are some of the functions in Python that allow you to read and write to files: read() : This function reads the entire file and returns. While to write to a file in Python, you need the file to be open in write mode. To write a file object returned by the built-in function open () or by popen () or fdopen (), or sys.stdout or sys.stderr, use its write () method. To read a file, you need to open the file in the read or write mode. According to the os.write documentation: Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open () or pipe (). You indicate that you want to keep listening for writes on the pipe, presumably even after a writer has closed. One important thing to note is the file operations mode. from contextlib import ExitStack filenames file1.txt, file2.txt, file3.txt with open ('outfile.txt', 'a') as outfile: with ExitStack () as stack: filepointers stack.entercontext (open (file, 'r. You should detect that case and break out of your loop: Sometimes, you might want to open a variable amount of files and treat each one the same, you can do this with contextlib. In your case, fifo.read() is returning an empty string, because the writer has closed its file descriptor.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |