The pty module, reading from a pty, and Python 2/3
David Hutto
dwightdhutto at gmail.com
Wed Oct 24 02:40:44 EDT 2012
#better coded for you to understand
import sys
import pty
import os
def get_text(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 need to find the file size, and place it as an integer in read
below, where you return the value
#you have to input into read, how many characters you want read in
with statinfo.st_size
statinfo = os.stat(filename)
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
More information about the Python-list
mailing list