Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'except:': 0.07; 'try:': 0.07; '"w")': 0.09; 'filename)': 0.09; 'oserror': 0.09; 'spawn': 0.09; 'subject:module': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; 'size,': 0.13; 'pty': 0.16; 'received:209.85.216.53': 0.16; 'sys.exit(1)': 0.16; 'integer': 0.17; 'input': 0.18; 'skip:p 30': 0.20; 'import': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'subject:/': 0.28; 'coded': 0.29; 'read,': 0.29; 'file': 0.32; 'received:google.com': 0.34; 'received:209.85': 0.35; 'except': 0.36; 'characters': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'where': 0.40; 'header:Received:5': 0.40; 'below,': 0.60; 'subject:, ': 0.61; 'subject:The': 0.71 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=ZA6pWbUr0DIdPBKdayWy9V3evofrqTZvA5U1pgzwyV4=; b=inH+hFKqrDBYyQuhFhtZAHyXWMzAddTo2kjwaLQ1yuVil4BlKu5SxFVyMvHH7soyZ1 s0eJDqCzmVwO5UK92U+5ios+A9QPp5I7qKBcfnUBR7z/HCWEOYNyW9jwVaoRn+AZekY7 lCLJ4XunCjEjnvaKnA2kpmy0PGo9ah3Bv5VmxoAU1lMn6pTZ17Ae3ccqTApnTwaAU0XZ XwTt3M0pTe1YyIVt00iRK7VKp9ZuZxwObU5qByUjZ2LQvH3wum/Mr7JuJz9jorebeodp JNErkdHP3YCZy/Svl47nKSy7OH2SZszBEoy2bPpIlFDcM1mIhKG0Pe+Y+MqgGvgdfdur Ozbg== MIME-Version: 1.0 In-Reply-To: <508759A9.7070601@wisc.edu> References: <508759A9.7070601@wisc.edu> Date: Wed, 24 Oct 2012 02:40:44 -0400 Subject: Re: The pty module, reading from a pty, and Python 2/3 From: David Hutto To: Evan Driscoll Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351060847 news.xs4all.nl 6911 [2001:888:2000:d::a6]:41679 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32007 #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