推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
rockyaow
V2EX  ›  Python

stdin 如何能自动判断

  •  
  •   rockyaow · Jul 29, 2014 · 4242 views
    This topic created in 4484 days ago, the information mentioned may be changed or developed.
    我写了个脚本,想支持用Linux 管道(|)方式传参数进去

    但是如果直接用sys.stdin.read去读,程序就卡住了,显然这个方法是不正确的。

    怎么能自动判断调用脚本的时候是否使用了管道?从而再去读stdin呢?
    4 replies  •  2014-07-30 10:15:18 +08:00
    qq529633582
    qq529633582
    Jul 29, 2014❤️1
    1
    isTTY()
    heiher
    heiher
    Jul 29, 2014 via iPhone
    2
    可以要求参数为-的时候读stdin,如 vim a.txt 访问文件,而 vim - 就访问 stdin。
    bombless
    bombless
    Jul 30, 2014
    3
    同意@heiher 的。
    判断是不是管道比较像Windows风格而不是*nix
    rockyaow
    rockyaow
    OP
    Jul 30, 2014
    4
    谢谢各位,sys.stdin.isatty 确认可行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1485 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:49 · PVG 13:49 · LAX 21:49 · JFK 00:49
    ♥ Do have faith in what you're doing.