python中执行linux命令方法1

在很多时候,我们都需要用到系统本身的命令,这个时候我们就需要有下面的知识储备了

[root@bogon IP]# vim bash.py

!/usr/bin/env python

import os
bash = os.system(“ls -al /root/”)
print bash

执行查看效果:

[root@bogon IP]# chmod u+x bash.py
[root@bogon IP]# python bash.py
total 168
dr-xr-x—. 13 root root 4096 Jan 11 09:07 .
dr-xr-xr-x. 22 root root 4096 Jan 9 18:43 ..
-rw——-. 1 root root 3318 Jan 9 04:40 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jan 9 19:16 apache
-rw——-. 1 root root 13225 Jan 11 06:52 .bash_history
-rw-r–r–. 1 root root 18 May 20 2009 .bash_logout
-rw-r–r–. 1 root root 295 Jan 10 00:58 .bash_profile
-rw-r–r–. 1 root root 176 Sep 22 2004 .bashrc
drwx——. 3 root root 4096 Jan 10 00:52 .cache
-rw-r–r–. 1 root root 100 Sep 22 2004 .cshrc
drwxr-xr-x. 7 root root 4096 Jan 10 02:15 django

    原文作者:付炜超
    原文地址: https://www.jianshu.com/p/7f47e2b3ca3e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞