1. 程式人生 > >Python系統管理

Python系統管理

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


In [13]: os.path.sep

Out[13]: '/'


In [14]: os.path.exists('1.txt')

Out[14]: True


In [15]: os.path.isdir('test')

Out[15]: True


In [16]: os.path.isfile('test')

Out[16]: False


In [17]: os.path.realpath('functions.sh')

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


In [18]: os.path.abspath('functions.sh')

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)

<ipython-input-29-c4255042d84c> in <module>()

----> 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]: <module 'posixpath' from '/root/.pyenv/versions/3.6.4/lib/python3.6/posixpath.py'>


In [82]: sys.stderr

Out[82]: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>


In [83]: sys.stdin

Out[83]: <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>


In [84]: sys.stderr

Out[84]: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>


In [85]: sys.exit

Out[85]: <function sys.exit>


Python系統管理