In the Python documentation I came across this method for objects like:
archivo = open('archivo', 'r');
archivo.isatty()
which returns True
/ False
whether the file is a tty or not. and i was wondering
- What is a tty file?
- What is a tty file used for?
file.isatty()
Return
True
if the file is connected to a tty(-like) device, elseFalse
.Note: If a file-like object is not associated with a real file, this method should not be implemented.
https://docs.python.org/2/library/stdtypes.html?highlight=file%20flush#file.isatty
The function
file.isatty
( TTY stands for TeleTypewriter , which are electromechanical teleprinters used in the past to connect to terminals), tells you if the file is connected to a TTY-type device.In Linux, you can find out the name of the file connected to your
STDIN
(Standard Input) using the commandtty
:The file
/dev/pts/24
should be connected to a virtual device:Actually the files
/dev/pts/N
are pseudo-terminals, that is, those that are created when you have a session from a graphical interface (for example, usinggnome-terminal
). If you connect to a virtual terminal (the ones you use in full screen) using Ctrl+ Alt+ F1, you can see that the commandtty
shows you a different file:The file
/dev/tty1
should also be connected to a TTY device:What is a TTY file?
In general, the operating system creates some files that allow you to communicate with physical devices, normally these files are located in the folder
/dev
and are treated in a special way since they are allowed to have access to the drivers. TTY files are responsible for displaying information input and output to the terminal (via the keyboard and monitor). You can see a list of them using:What is a TTY file used for?
Well, as I mentioned in the first question, they are used by the operating system to allow you to "converse" with physical devices like the keyboard and monitor. Maybe this image will help a bit:
References
tty
it is a physical terminal, with keyboard and screen, or virtual like XTerm. Unlike a normal file, each tty has "properties" such as size (dimensions, width and height), the ability to change colors, and others. In Unix and similar systems like Linux each terminal (physical or virtual) connects to the virtual file, its owntty
. They can be found at/dev/
:/dev/tty<N>
they are for text consoles, and/dev/pts/<N>
for terminal emulators like XTerm or Gnome Terminal.A program can find out that its
stdout
andstderr
correspond to atty
and activate a special mode, for eg. eg create an interactive interface with "windows" asMidnight Commander
in Linux/Unix orFAR Commander
in Windows.In Python it can be used like this:
This code writes the text "Hello World!" in red using ANSI escape sequences (in English) if it is executed simply in
bash
, but withstdout
redirected to a file, it writes the text without special characters.It is also possible to send something to a
tty
andtty
it will receive it. write and other programs use this feature to send messages to users.