Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 23rd, 2012  |  syntax: Python  |  size: 0.69 KB  |  views: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. import sys
  3. import pty
  4. import os
  5.  
  6. def get_text(filename):
  7.     try:
  8.         ( child_pid, fd ) = pty.fork()    # OK
  9.     except OSError as e:
  10.         print(str(e))
  11.         sys.exit(1)
  12.  
  13.     if child_pid == 0:
  14.         try:
  15.             with open("log.txt", "w") as f:
  16.                 f.write("about to execlp")
  17.             os.execlp("cat", "cat", filename)
  18.         except:
  19.             with open("log.txt", "w") as f:
  20.                 f.write("could not spawn process")
  21.             print("Could not spawn")
  22.             sys.exit(1)
  23.  
  24.     child_pty = os.fdopen(fd)
  25.     return child_pty.read()
  26.    
  27.  
  28. if __name__ == "__main__":
  29.     print(get_text("my-pty-test.py"))
clone this paste RAW Paste Data