automating control-D

Steve Holden sholden at holdenweb.com
Sat Jul 27 00:28:33 EDT 2002


"Victor Chen" <vchen at meetingmaker.com> wrote in message
news:mailman.1027708758.17684.python-list at python.org...
> Hi,
>     I'm fairly new a python stuff, working on my first python script.
> I was wondering if anyone knows how I can represent
> control-D as output using python. Firstly, I do not know
> the special string used to represent the control key and secondly, I
> would have to automate both the control and the d characters
> be pressed together. Any help is appreciated. Thank you.

In Python such characters are best represented as octal or hexadecimal
escapes. Since the ordinal value of ^D is 4, you could use either "\004" or
"\x04" to represent the character.

Your parting shot, however, makes it appear that you are perhaps thinking of
creating input to some other program. If this is indeed the case, you should
be aware that if you are using conventional means such as pipes to
communicate with another program then you shouldn't send a "^D" character,
but instead simply close the file you are using to send data to the other
program.

When you enter "^D" in a Unix terminal input this is tasken (by the tty
handler) to indicate end of input. If no readabl;e characters preceded this
condition since the last read, this is taken as indicating end of file.

On the third hand, if you want to have a Python program simulate keyboard
input to a whole operating system (which you might, since you talk about
pressing keys to create keystrokes), this is a couple of orders of magnitude
more complex.

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------









More information about the Python-list mailing list