Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตรวจสอบว่าไฟล์เชื่อมต่อกับ Terminal หรือไม่ใน Python

ใช้ isatty() ตรวจสอบว่า script รันใน terminal หรือถูก pipe

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

รันจาก terminal จะได้:

"Inside a terminal!"