Python 3 – File isatty() Method
In Python 3, the isatty()
method is used to check whether the file descriptor is associated with a terminal or not. A terminal is a device that allows the user to interact with the computer through a command-line interface.
Syntax:
file.isatty()
The isatty()
method takes no arguments and returns a Boolean value. If the file descriptor is associated with a terminal, it returns True
. Otherwise, it returns False
.
If the file descriptor is a standard input, output, or error stream, we can use sys.stdin
, sys.stdout
, or sys.stderr
objects to check its association with the terminal. We can use the isatty()
method to check whether the input, output, or error stream is connected to a terminal or not.
Example:
import sys
# check if stdin is associated with a terminal
if sys.stdin.isatty():
print('stdin is connected to a terminal')
else:
print('stdin is NOT connected to a terminal')
# check if stdout is associated with a terminal
if sys.stdout.isatty():
print('stdout is connected to a terminal')
else:
print('stdout is NOT connected to a terminal')
# check if stderr is associated with a terminal
if sys.stderr.isatty():
print('stderr is connected to a terminal')
else:
print('stderr is NOT connected to a terminal')
Output:
$ python3 test.py
stdin is NOT connected to a terminal
stdout is connected to a terminal
stderr is connected to a terminal
Here, we have imported the sys
module to access the standard input, output, and error streams. We have used the isatty()
method to check the association of these streams with the terminal. We can see that only the output and error streams are connected to the terminal, while the input stream is not.
We can also use the isatty()
method with file objects to check their association with the terminal.
Example:
# open a file in read mode
f = open('test.txt', 'r')
# check if the file is associated with a terminal
if f.isatty():
print('file is connected to a terminal')
else:
print('file is NOT connected to a terminal')
# close the file
f.close()
Output:
$ python3 test.py
file is NOT connected to a terminal
Here, we have opened a file named test.txt
in read mode and checked its association with the terminal using the isatty()
method. Since the file is not connected to any terminal, the method returns False
.
We can also use the isatty()
method with other file modes like write mode, append mode, etc.
Example:
# open a file in write mode
f = open('output.txt', 'w')
# check if the file is associated with a terminal
if f.isatty():
print('file is connected to a terminal')
else:
print('file is NOT connected to a terminal')
# close the file
f.close()
Output:
$ python3 test.py
file is NOT connected to a terminal
Here, we have opened a file named output.txt
in write mode and checked its association with the terminal using the isatty()
method. Since the file is not connected to any terminal, the method returns False
.
Conclusion
The isatty()
method is a useful method to check whether the file descriptor is associated with a terminal or not. We can use it with standard input, output, and error streams, as well as with other file modes to determine their association with the terminal.