Python 3 + Linux terminal

Started by
4 comments, last by FableFox 10 years ago

How do I write to a terminal using Python under linux?

For example in Ubuntu you can open two terminal, type 'tty' to get the handle. and you can pipe between them. for example you can type in one but the output get out on the other one.

So the question is...

1. how do I open a new terminal in Python and already have the handle?

2. or if I already have a terminal window open, provide this to python?

3. how to I write to terminal? including newline?

for example this:

-a person open a terminal window

-run my app

-provide the tty handle to my app

-my app ask for name

-print out "Hello name" in the terminal window.

-and provide a new line / enter

-so that I can also send 'echo 'name' to the terminal and press enter.

I'm new to python but I've been googling but can't even find a simple app that print hello world to a terminal window in linux. you know, something as simple as:

-ask for terminal tty handle

-print hello world in the terminal

is it tty? but python lib is not helping and it seems tty can be used for text files too?

I already google but either tty in python is too enriched or what I have no idea.

Advertisement

Python's print writes to standard out, which is written to the terminal, so I don't get what you mean?

Save this as "test.py":


#!/usr/bin/env python
print('hello world')

When I run it, "hello world" is written to stdout:


$ chmod +x test.py
$ ./test.py
hello world
$
"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

Python's print writes to standard out, which is written to the terminal, so I don't get what you mean?

Save this as "test.py":


#!/usr/bin/env python
print('hello world')

When I run it, "hello world" is written to stdout:


$ chmod +x test.py
$ ./test.py
hello world
$

I want to create a GUI app that can stream text to a linux terminal, including carriage return / enter.

So I can create a window with a 'hello word', and when user open a linux terminal, and supply that linux terminal tty, clicking hello world will stream "echo 'hello world' and automatically presses enter, making linux to print 'Hello World'.

Long story, but that is the main idea.

To open a new terminal from a python application you must start it in a new subprocess:

subprocess.Popen(['gnome-terminal', '-e'])
To communicate with the opened terminal you may want to use the pexpect library, it will help you write to the terminal stdin and get the terminal stdout.

That being said, I have no idea how you can retrieve an existing tty handle and write to its stdout.

To open a new terminal from a python application you must start it in a new subprocess:

subprocess.Popen(['gnome-terminal', '-e'])
To communicate with the opened terminal you may want to use the pexpect library, it will help you write to the terminal stdin and get the terminal stdout.

That being said, I have no idea how you can retrieve an existing tty handle and write to its stdout.

os.ttyname(filedescriptor) gives you the ttyhandle, os.ttyname(sys.stdout) from the process owning the terminal should work (if the terminal is connected directly to stdout).

os.open(ttyname, flags) should provide a filehandle for reading/writing to a named terminal.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

To open a new terminal from a python application you must start it in a new subprocess:

subprocess.Popen(['gnome-terminal', '-e'])
To communicate with the opened terminal you may want to use the pexpect library, it will help you write to the terminal stdin and get the terminal stdout.

That being said, I have no idea how you can retrieve an existing tty handle and write to its stdout.

Thanks for the pexpect library. It really help me in some of my ideas (ssh, for example).

This topic is closed to new replies.

Advertisement