Determining if stdout for a Python process is redirected

前端 未结 3 1299
难免孤独
难免孤独 2021-02-01 06:27

I\'ve noticed that curl can tell whether or not I\'m redirecting its output (in which case it puts up a progress bar).

Is there a reasonable way to do this in a Python s

3条回答
  •  日久生厌
    2021-02-01 06:56

    import sys
    
    if sys.stdout.isatty():
        print "Not redirected"
    else:
        sys.stderr.write("Redirected!\n")
    

提交回复
热议问题