title: When working with a named pipe is there a way to do something like readlines()
body: <p>Overall Goal: I am trying to read some progress data from a python exe to update the progress of the exe in another application</p>
  
  <p>I have a python exe that is going to do some stuff, I want to be able to communicate the progress to another program. Based on several other Q&amp;A here I have been able to have my running application send progress data to a named pipe using the following code</p>
  
  <pre><code>import win32pipe
  import win32file
  import glob
  test_files = glob.glob('J:\\someDirectory\\*.htm')
  # test_files has two items a.htm and b.htm
  p = win32pipe.CreateNamedPipe(r'\\.\pipe\wfsr_pipe',
                            win32pipe.PIPE_ACCESS_DUPLEX,
                            win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                            1,65536,65536,300,None)
  # the following line is the server-side function for accepting a connection
  # see the following SO question and answer
  """ http://stackoverflow.com/questions/1749001/named-pipes-between-c-sharp-and-python
  """
  win32pipe.ConnectNamedPipe(p, None)
  for each in testFiles:
      win32file.WriteFile(p,each + '\n')
  #send final message
  win32file.WriteFile(p,'Process Complete')
  # close the connection
  p.close()
  </code></pre>
  
  <p>In short the example code writes the path of the each file that was globbed to the NamedPipe - this is useful and can be easily extended to more logging type events.  However, the problem is trying to figure out how to read the content of the named pipe without knowing the size of each possible message.  For example the first file could be named J:\someDirectory\a.htm, but the second could have 300 characters in the name.</p>
  
  <p>So far the code I am using to read the contents of the pipe requires that I specify a buffer size</p>
  
  <p>First establish the connection</p>
  
  <pre><code>file_handle = win32file.CreateFile("\\\\.\\pipe\\wfsr_pipe",
                            win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                            0, None,
                            win32file.OPEN_EXISTING,
                            0, None)
  </code></pre>
  
  <p>and then I have been playing around with reading from the file</p>
  
  <pre><code>data = win32file.ReadFile(file_handle,128)
  </code></pre>
  
  <p>This generally works but I really want to read until I hit a newline character, do something with the content between when I started reading and the newline character and then repeat the process until I get to a line that has Process Complete in the line</p>
  
  <p>I have been struggling with how to read only until I find a newline character (\n).  I basically want to read the file by lines and based on the content of the line do something (either display the line or shift the application focus).</p>
  
  <p>Based on the suggestion provided by @meuh I am updating this because I think there is a dearth of examples, guidance in how to use pipes</p>
  
  <p>My server code  </p>
  
  <pre><code>import win32pipe
  import win32file
  import glob
  import os
  
  p = win32pipe.CreateNamedPipe(r'\\.\pipe\wfsr_pipe',
                            win32pipe.PIPE_ACCESS_DUPLEX,
                            win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                            1,65536,65536,300,None)
  # the following line is the server-side function for accepting a connection
  # see the following SO question and answer
  """ http://stackoverflow.com/questions/1749001/named-pipes-between-c-sharp-and-python
  """
  win32pipe.ConnectNamedPipe(p, None)
  for file_id in glob.glob('J:\\level1\\level2\\level3\\*'):
      for filer_id in glob.glob(file_id + os.sep + '*'):
          win32file.WriteFile(p,filer_id)    
  #send final message
  win32file.WriteFile(p,'Process Complete')
  
  # close the connection
  p.close()  #still not sure if this should be here, I need more testing
  # I think the client can close p
  </code></pre>
  
  <p>The Client code  </p>
  
  <pre><code>import win32pipe
  import win32file
  
  file_handle = win32file.CreateFile("\\\\.\\pipe\\wfsr_pipe",
                                 win32file.GENERIC_READ |
                                 win32file.GENERIC_WRITE,
                                 0, None,win32file.OPEN_EXISTING,0, None)
  # this is the key, setting readmode to MESSAGE
  win32pipe.SetNamedPipeHandleState(file_handle,
                                win32pipe.PIPE_READMODE_MESSAGE, None, None)
  # for testing purposes I am just going to write the messages to a file
  out_ref = open('e:\\testpipe.txt','w')
  
  dstring = ''  # need some way to know that the messages are complete
  while dstring != 'Process Complete':
      # setting the blocksize at 4096 to make sure it can handle any message I
      # might anticipate
      data = win32file.ReadFile(file_handle,4096)
  # data is a tuple, the first position seems to always be 0 but need to find 
  # the docs to help understand what determines the value, the second is the 
  # message
      dstring = data[1]
      out_ref.write(dstring + '\n')
  
  out_ref.close() # got here so close my testfile
  file_handle.close() # close the file_handle
  </code></pre>