Execute system command method in Python environment

Source: Internet
Author: User
Tags syslog

Method 1:os.system>>>import os>>>os.system (' ls ') anaconda-ks.cfg  django-1.2.7   install.log.syslog  ptyprocess-0.5.1    server1.py      Public      Document a.py         docker         mysite            pycharm-2016.3.2    server.py     Templates      Download client1.py     docker-1.7.1  paramiko-2.1.1        pycharm-license.txt    setuptools-2.0     Video       Music client.py     install.log   pexpect-4.2.1        pycrypto-2.6.1    spawn-0.1     Pictures       Desktop 0>>> SquareFA 2:os.popen>>>import os>>>tmp = os.popen (' ls ') >>>tmp.readlines () anaconda-ks.cfg  django-1.2.7  install.log.syslog  ptyprocess-0.5.1     server1.py     Public      Document a.py          docker        mysite            pycharm-2016.3.2    server.py      Templates      Download client1.py     docker-1.7.1   paramiko-2.1.1       pycharm-license.txt     setuptools-2.0     Video      Music client.py      install.log   pexpect-4.2.1       pycrypto-2.6.1     spawn-0.1     Pictures     Desktop 0>>> Method 3:subprocess>>> import subprocess>>>  subprocess.call ((' ls '), shell=true) Anaconda-ks.cfg  django-1.2.7  install.log.syslog   ptyprocess-0.5.1    server1.py     Public       Document a.py         docker         mysite           pycharm-2016.3.2     server.py     templates      Download client1.py      docker-1.7.1  paramiko-2.1.1        pycharm-license.txt    setuptools-2.0     Video      Music client.py     install.log   pexpect-4.2.1        pycrypto-2.6.1    spawn-0.1     Pictures      Desktop 0>>>>>>p =  Subprocess. Popen (' ls ', shell=true,stdout=subprocess. Pipe,stderr=subprocess. STDOUT) >>>p.stdout.readlines (): [' anaconda-ks.cfg\n ',  ' a.py\n ',  ' client1.py\n ',  ' client.py\n ',  ' django-1.2.7\n ',  ' docker\n ',  "docker-1.7.1\n ',  ' install.log\n ',  ' install.log.syslog\n ',  ' mysite\n ',  ' paramiko-2.1.1\n ',  ' pexpect-4.2.1\n ',  ' ptyprocess-0.5.1\n ',  ' pycharm-2016.3.2\n ',  ' pycharm-license.txt\n ',  ' pycrypto-2.6.1\n ',  ' server1.py\n ',  ' server.py\n ',  ' setuptools-2.0\n ',  ' spawn-0.1\n ',  ' \xe5\x85\xac\xe5\x85\xb1 \xe7\x9a\x84\n ',  ' \xe6\xa8\xa1\xe6\x9d\xbf\n ',  ' \xe8\xa7\x86\xe9\xa2\x91\n ',  ' \xe5\x9b\xbe\xe7\ x89\x87\n ',  ' \xe6\x96\x87\xe6\xa1\xa3\n ',  ' \xe4\xb8\x8b\xe8\xbd\xbd\n ',  ' \xe9\x9f\xb3\xe4\xb9\ x90\n ',  ' \xe6\xa1\x8c\xe9\x9d\xa2\n '] method 4:commands>>>import coMmands>>>commands.getoutput (' date ') >>> ' 2017\xe5\xb9\xb4 05\xe6\x9c\x88 08\xe6\x97\ XA5 \XE6\X98\X9F\XE6\X9C\X9F\XE4\XB8\X80 18:26:25 CST ' >>>commands.getstatusoutput (' Date ') >>> (0,  ' 2017\xe5\xb9\xb4 05\xe6\x9c\x88 08\xe6\x97\xa5 \xe6\x98\x9f\xe6\x9c \X9F\XE4\XB8\X80 18:27:24 CST ')


This article is from the "GSWCFL" blog, make sure to keep this source http://guoshiwei.blog.51cto.com/2802413/1924267

Execute system command method in Python environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.