• Advertisement
Sign in to follow this  

Python 3 + Linux terminal

This topic is 1375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

 

 

 

 

 

Share this post


Link to post
Share on other sites
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
$

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement