Running shell commands within python interpreter

This question already has an answer here:

  • Calling an external command in Python 50 answers

  • In plain python, you need to use something along the lines of this:

    from subprocess import check_output
    check_output("ls", shell=True)
    

    In IPython, you can run either of those commands or a general shell command by starting off with ! . For example

    ! echo "Hello, world!" > /tmp/Hello.txt
    

    If you're using python interactively, you would almost certainly be happier with IPython.


    如果您打算在调用命令(ls,pwd,...)时交互使用Python shell,请查看iPython。

    链接地址: http://www.djcxy.com/p/13510.html

    上一篇: 如何在python脚本中运行我自己的外部命令

    下一篇: 在python解释器中运行shell命令