推荐学习书目
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 · 3819 views
    This topic created in 4320 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
        1
    qq529633582  
       Jul 29, 2014   ❤️ 1
    isTTY()
    heiher
        2
    heiher  
       Jul 29, 2014 via iPhone
    可以要求参数为-的时候读stdin,如 vim a.txt 访问文件,而 vim - 就访问 stdin。
    bombless
        3
    bombless  
       Jul 30, 2014
    同意@heiher 的。
    判断是不是管道比较像Windows风格而不是*nix
    rockyaow
        4
    rockyaow  
    OP
       Jul 30, 2014
    谢谢各位,sys.stdin.isatty 确认可行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    ♥ Do have faith in what you're doing.