Python标准库01

本文主要介绍Python标准库。

1.os模块

  • 操作文件
    以下内容在shell终端操作
    1
    2
    3
    4
    5
    6
    # touch 1.txt						//创建文件
    # ls
    1.txt
    # echo "os module" > 1.txt //写入内容
    # cat 1.txt
    os module

以下是python终端中操作

1
2
3
4
5
6
7
8
# python							//进入python命令行
>>> import os //导入os模块
>>> dir(os) //获得os模块的帮助
>>> os.rename('1.txt','2.txt') //将1.txt重命名为2.txt
>>> os.remove('2.txt') //删除2.txt文件
>>> os.mknod('test.txt') //创建一个空文件
>>> os.stat('test.txt') //获取文件属性
>>> os.chmod('test.txt',0666) //修改文件权限

  • 操作目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    >>> os.getcwd()						//获取当前目录
    '/python'
    >>> os.listdir('/python') //列出文件和目录
    ['test.txt']
    >>> os.mkdir('foo') //创建目录
    >>> os.listdir('.')
    ['test.txt', 'foo']
    >>> os.rmdir('foo') //删除目录

    >>> os.mkdir('foo') //重新创建目录
    >>> os.chdir('foo') //进入foo目录
    >>> os.getcwd() //获取当前目录
    '/python/foo'

    >>> os.makedirs('a/b/c') //创建多级目录
    >>> os.removedirs('a/b/c') //删除多级目录

    >>> os.getcwd() //当前目录
    '/python'
    >>> os.listdir('.') //当前目录文件列表
    ['test.txt', 'foo']
    >>> os.chdir('foo') //进入foo目录
    >>> os.mknod('1.txt') //创建文件,此时foo目录不为空
    >>> os.chdir('..') //返回上一级目录
    >>> os.getcwd()
    '/python'
    >>> os.rmdir('foo') //使用rmdir删除会报错,怎么删除?
    >>> import shutil
    >>> shutil.rmtree('foo') //删除非空目录
  • 系统相关

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    >>> os.sep					//操作系统特定的路径分隔符
    '/'
    >>> os.name //正在使用的工作平台,Windows是'nt',Linux/Unix是'posix'
    'posix'
    >>> os.getenv('PATH') //读取和设置环境变量:os.getenv() 与os.putenv()
    '/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'
    >>> os.linesep //当前平台使用的行终止符
    '\n'
    >>> os.system('ls -l') //调用系统命令
    >>> os.pathsep
    ':'
    >>> os.extsep
    '.'
  • os.path模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # ls -l
    drwxr-xr-x 2 root root 4096 Oct 23 16:30 foo
    -rw-rw-rw- 1 root root 0 Oct 23 13:15 test.txt
    //当前目录下一个目录和一个文件
    # python //进入python终端
    >>> import os
    >>> os.path.isfile('test.txt') //判断是不是文件
    True
    >>> os.path.isdir('foo') //判断是不是目录
    True
    >>> os.path.isdir('test.txt')
    False
    >>> os.path.split('/python/foo') //返回路径的目录和文件名
    ('/python', 'foo')
    >>> os.path.split('/python/foo/')
    ('/python/foo', '')
    >>> os.path.getsize('test.txt') //返回文件大小,如果为目录,返回0
    1551
    >>> os.path.abspath('.') //获得绝对路径
    '/python'
    >>> os.path.join('/python','test.txt') //连接目录和文件名
    '/python/test.txt'
    >>> os.path.basename('/usr/bin/env') //返回文件名
    'env'
    >>> os.path.dirname('/usr/bin/env') //返回文件路径
    '/usr/bin'
    >>> os.path.splitext('test.txt') //分离文件名和扩展名
    ('test', '.txt')

2.shutil模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
>>> shutil.copyfile('/root/nginx.conf','./nginx.conf')		//复制文件
>>> shutil.copy('/root/nginx.conf','./test.txt') //复制数据
>>> shutil.copytree('/opt/bar','/python/bar') //递归复制文件夹
>>> shutil.move('/python/bar','/root/') //移动目录
>>> shutil.rmtree('/root/bar') //删除目录

# ls -l
drwxr-xr-x 2 root root 4096 Oct 23 16:30 foo
-rw-rw-rw- 1 root root 1258 Oct 23 16:51 nginx.conf
-rw-r--r-- 1 root root 1258 Oct 23 16:52 test.txt

>>> import shutil
>>> shutil.copymode('nginx.conf','test.txt') //复制权限

# ls -l
drwxr-xr-x 2 root root 4096 Oct 23 16:30 foo
-rw-rw-rw- 1 root root 1258 Oct 23 16:51 nginx.conf
-rw-rw-rw- 1 root root 1258 Oct 23 16:52 test.txt

>>> shutil.copystat('nginx.conf','test.txt') //复制属性
>>> shutil.copy2('nginx.conf','test.txt') //先copyfile后copystat

make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None) #压缩打包
base_name: 压缩打包后的文件名或者路径名
format: 压缩或者打包格式 "zip", "tar", "bztar"or "gztar"
root_dir : 将哪个目录或者文件打包(也就是源文件)

>>> shutil.make_archive('python','gztar',root_dir='/python/')
'/python/python.tar.gz'

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile

# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()

import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall() # 可设置解压地址
tar.close()

3.sys模块

sys.argv
功能:在外部向程序内部传递参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# cat argv.py 
#!/usr/bin/env python

import sys
print sys.argv[0]
print sys.argv[1]

# python argv.py aa
argv.py
aa

# cat argv.py
#!/usr/bin/env python
#coding:utf-8

import sys

if len(sys.argv)>1:
print "传递了",len(sys.argv)-1,"个参数"

for arg in sys.argv[1:]:
print arg
else:
print "没有传参"
# python argv.py
没有传参
# python argv.py 11 22 33 aa bb cc
传递了 6 个参数
11
22
33
aa
bb
cc

sys.exit(n)
功能:解释器自动退出
>>> import sys
>>> sys.exit(0)

>>> sys.getdefaultencoding() //获取系统当前编码
'ascii'
>>> sys.getfilesystemencoding() //获取文件系统使用编码方式
'UTF-8'
>>> sys.path //获取指定模块搜索路径的集合
>>> sys.platform //获取当前系统平台
'linux2'

sys.stdin,sys.stdout,sys.stderr //标准I/O

4.platform模块

1
2
3
4
5
6
7
8
>>> import platform
>>> platform.platform() //获取操作系统名称及版本号
>>> platform.version() //获取操作系统版本号
>>> platform.architecture() //获取操作系统的位数
>>> platform.machine() //计算机类型
>>> platform.node() //计算机的网络名称
>>> platform.processor() //计算机处理器信息
>>> platform.uname() //包含上面所有的信息汇总
---------------- The End ----------------