Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:21:11+00:00 2026-06-14T13:21:11+00:00

In a simple Python script intended to be run from the shell, can I

  • 0

In a simple Python script intended to be run from the shell, can I reliably determine whether sys.stdin has been redirected from an actual file vs. piped from another process?

I want to change runtime behavior depending on whether stdin is coming from a data file vs. streaming from another process via a pipe.

As expected, isatty() returns False in both cases. Here’s a quick isatty() test:

# test.py
import os
import sys
print sys.stdin.isatty()
print os.isatty(sys.stdin.fileno())

Testing:

python test.py < file.txt

produces:

False
False

and:

ls -al | python test.py

produces:

False
False

Is there a pythonic method of doing this?

Unix/Linux specific is fine, though it would be nice to know if it’s possible to do this in a portable manner.

Edit: Note in response to commenters: Why do I care? Well, in my case, I want to deal with time-stamped data that comes in at irregular intervals when piped from another process; when I play back pre-recorded data from a file, I’d like to replay it using a fixed or variable delays.

I agree it’s probably advantageous to use a cleaner method (and I can think of several, including an intermediate script to insert delays in the playback stream) but I’m terminally curious.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:21:12+00:00Added an answer on June 14, 2026 at 1:21 pm

    You’re looking for stat macros:

    import os, stat
    
    mode = os.fstat(0).st_mode
    if stat.S_ISFIFO(mode):
         print("stdin is piped")
    elif stat.S_ISREG(mode):
         print("stdin is redirected")
    else:
         print("stdin is terminal")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions about the performance of this simple python script: import sys,
I'm trying to run an Icecast stream using a simple Python script to pick
I'm trying to get a simple python script from the learnpythonthehardway tutorial to show
I'm working on a simple Python script that can use subprocess and/or os to
I have a simple python script that i wrote for IDA, but i'am can't
I'm trying to run a simple OpenCV python script that uses the samples/python2/facedetect.py example,
I was writing a simple python script to read from and reconstruct data from
Simple python script: for i in range(0, 5): print ok sys.stderr.write('err\r\n') When executing this
I have a super simple python script as defined here import multiprocessing from multiprocessing
I have a simple Python script that runs in a console windows. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.