Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách kiểm tra file kết nối terminal trong Python?


Kiểm tra script hiện tại có kết nối terminal hay không dùng isatty(). Ví dụ:

import sys
if sys.stdout.isatty():
    print("Inside a terminal!")
else:
    print("Piped output")

Chạy từ terminal, nhận output:

"Inside a terminal!"