{"id":214,"date":"2011-07-25T11:00:06","date_gmt":"2011-07-25T16:00:06","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=214"},"modified":"2011-07-25T11:26:34","modified_gmt":"2011-07-25T16:26:34","slug":"how-to-detect-stdin-with-python","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2011\/07\/25\/how-to-detect-stdin-with-python\/","title":{"rendered":"HOW TO detect STDIN with Python"},"content":{"rendered":"<p>In Python, as with most languages, STDIN is treated like a file.  You can read from it any time, but knowing what kind of file it is lets us know if someone passed us something via STDIN or if we are going to prompt them for it.<\/p>\n<p><code>import os, sys<\/p>\n<p>if os.isatty(file.fileno(sys.stdin)):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Reading list from STDIN.\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Enter your list. Press ^D to continue, ^C to quit.\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;my_list = sys.stdin.readlines()<br \/>\nelse:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Thank you for passing me a list through a pipe.\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;my_list = sys.stdin.readlines()<\/code><\/p>\n<p>Now lets try it two ways:<\/p>\n<p>First, pass it some stuff:<br \/>\n<code>echo mother sister father brother | .\/myapp.py<\/code><\/p>\n<p>Then try it plain:<br \/>\n<code>.\/myapp.py<\/code><\/p>\n<blockquote><p>Did you find this post useful or have questions or comments?  Please let  me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>In Python, as with most languages, STDIN is treated like a file. You can read from it any time, but knowing what kind of file it is lets us know if someone passed us something via STDIN or if we &hellip; <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2011\/07\/25\/how-to-detect-stdin-with-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,9,59],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-programming","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":9,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/214\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}