Python系统管理

在Linux操作系统上编写shell脚本时, 主要涉及到执行系统命令, 目录和文件操作. 那么在python中怎么办呢, 这主要是几个模块的使用: os, os.path, shutil和sys...

os模块包含了大部分的Linux命令, 简单举例如下:

In [1]: import os

In [2]: os.system('ls -l')

total 12

-rw-r--r-- 1 root root 107 Mar 1 19:31 1.txt

-rw-r--r-- 1 root root 214 Mar 1 19:30 2.txt

lrwxrwxrwx 1 root root 21 Mar 1 22:13 functions.sh -> /etc/init.d/functions

drwxr-xr-x 3 root root 4096 Mar 1 22:16 test

Out[2]: 0

In [4]: os.listdir('.')

Out[4]: ['1.txt', 'functions.sh', '2.txt', 'test']

In [5]: os.chdir('test')

In [6]: os.getcwd()

Out[6]: '/tmp/test'

os.path模块主要是目录和文件操作, 简单举例如下:

In [12]: import os.path

Out[13]: '/'

Out[14]: True

Out[15]: True

Out[16]: False

Out[17]: '/etc/rc.d/init.d/functions'

Out[18]: '/tmp/functions.sh'

In [19]: os.path.split('/etc/rc.d/init.d/functions')

Out[19]: ('/etc/rc.d/init.d', 'functions')

In [21]: os.path.splitext('/tmp/functions.sh')

Out[21]: ('/tmp/functions', '.sh')

In [22]: os.path.dirname('/tmp/functions.sh')

Out[22]: '/tmp'

In [23]: os.path.basename('/tmp/functions.sh')

Out[23]: 'functions.sh'

In [25]: os.path.join('/tmp/test', 'stage')

Out[25]: '/tmp/test/stage'

shutil模块是高层次文件操作, 简单举例如下:

In [26]: import shutil

In [29]: os.rmdir('test')

---------------------------------------------------------------------------

OSError Traceback (most recent call last)

in ()

----> 1 os.rmdir('test')

OSError: [Errno 39] Directory not empty: 'test'

In [30]: shutil.rmtree('test')

In [34]: shutil.copy('1.txt', '4.txt')

Out[34]: '4.txt'

In [41]: shutil.copytree('test', 'test_bak')

Out[41]: 'test_bak'

In [42]: shutil.move('test_bak', 'stage')

Out[42]: 'stage'

In [47]: shutil.get_unpack_formats()

Out[47]:

[('bztar', ['.tar.bz2', '.tbz2'], "bzip2'ed tar-file"),

('gztar', ['.tar.gz', '.tgz'], "gzip'ed tar-file"),

('tar', ['.tar'], 'uncompressed tar file'),

('zip', ['.zip'], 'ZIP file')]

In [48]: shutil.unpack_archive('tmux-2.6.tar.gz')

In [50]: shutil.make_archive('tmux26', 'zip', 'tmux-2.6')

Out[50]: '/tmp/tmux26.zip'

sys模块用于处理解释器运行时配置, 和操作系统环境资源交互的, 简单举例如下:

In [58]: import sys

In [59]: sys.argv

Out[59]: ['/root/.pyenv/versions/3.6.4/bin/ipython']

In [63]: sys.path

Out[63]:

['',

'/root/.pyenv/versions/3.6.4/bin',

'/root/.pyenv/versions/3.6.4/lib/python36.zip',

'/root/.pyenv/versions/3.6.4/lib/python3.6',

'/root/.pyenv/versions/3.6.4/lib/python3.6/lib-dynload',

'/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages',

'/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/IPython/extensions',

'/root/.ipython']

In [67]: sys.getdefaultencoding()

Out[67]: 'utf-8'

In [68]: sys.getfilesystemencoding()

Out[68]: 'utf-8'

In [79]: sys.modules['os.path']

Out[79]:

In [82]: sys.stderr

Out[82]: ' mode='w' encoding='UTF-8'>

In [83]: sys.stdin

Out[83]: ' mode='r' encoding='UTF-8'>

In [84]: sys.stderr

Out[84]: ' mode='w' encoding='UTF-8'>

In [85]: sys.exit

Out[85]:

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180301G1KFVS00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券