Jump to content

  • Log In with Google      Sign In   
  • Create Account

Python 3 + Linux terminal


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 FableFox   Members   -  Reputation: 633

Like
0Likes
Like

Posted 17 April 2014 - 03:18 AM

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.

 

 

 

 

 


Fable Fox is Stronger <--- Fable Fox is Stronger Project

#2 TheComet   Crossbones+   -  Reputation: 2516

Like
0Likes
Like

Posted 17 April 2014 - 04:44 AM

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
$

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#3 FableFox   Members   -  Reputation: 633

Like
0Likes
Like

Posted 17 April 2014 - 06:56 AM

 

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.


Fable Fox is Stronger <--- Fable Fox is Stronger Project

#4 ColinDuquesnoy   Members   -  Reputation: 1162

Like
2Likes
Like

Posted 17 April 2014 - 07:13 AM

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.



#5 SimonForsman   Crossbones+   -  Reputation: 7423

Like
3Likes
Like

Posted 17 April 2014 - 07:48 AM

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.


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!

#6 FableFox   Members   -  Reputation: 633

Like
1Likes
Like

Posted 17 April 2014 - 08:47 AM

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


Fable Fox is Stronger <--- Fable Fox is Stronger Project




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS