Re: The pty module, reading from a pty, and Python 2/3

Discussion in 'Python' started by David Hutto, Oct 24, 2012.

  1. David Hutto

    David Hutto Guest

    import sys
    import pty
    import os

    def get_text(filename):
    #you need to find the file size, and place it as an integer in read
    below, where you return the value
    statinfo = os.stat(filename)

    try:
    ( child_pid, fd ) = pty.fork() # OK
    except OSError as e:
    print(str(e))
    sys.exit(1)

    if child_pid == 0:
    try:
    with open("log.txt", "w") as f:
    f.write("about to execlp")
    os.execlp("cat", "cat", filename)
    except:
    with open("log.txt", "w") as f:
    f.write("could not spawn process")
    print("Could not spawn")
    sys.exit(1)

    child_pty = os.fdopen(fd)
    #you have to input into read, how many characters you want read in. if
    you place a random integer in, it will read to that integer within the
    file
    return child_pty.read(statinfo.st_size)


    if __name__ == "__main__":
    print(get_text("my-pty-test.py"))


    --
    Best Regards,
    David Hutto
    CEO: http://www.hitwebdevelopment.com
     
    David Hutto, Oct 24, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan Driscoll
    Replies:
    0
    Views:
    232
    Evan Driscoll
    Oct 24, 2012
  2. Evan Driscoll
    Replies:
    0
    Views:
    185
    Evan Driscoll
    Oct 24, 2012
  3. Evan Driscoll
    Replies:
    0
    Views:
    214
    Evan Driscoll
    Oct 24, 2012
  4. David Hutto
    Replies:
    0
    Views:
    222
    David Hutto
    Oct 24, 2012
  5. David Hutto
    Replies:
    0
    Views:
    149
    David Hutto
    Oct 24, 2012
Loading...

Share This Page