Python系统管理模块 语法风格及布局 字符串

python官方手册页:

https://docs.python.org/zh-cn/3/ -> 标准库参考

shutil模块

复制和移动

  • shutil.copyfileobj(fsrc, fdst[, length])
    将类似文件的对象fsrc的内容复制到类似文件的对象fdst。
  • shutil.copyfile(src, dst, *, follow_symlinks=True)
    将名为src的文件的内容(无元数据)复制到名为dst的文件,然后返回dst。

src = source 源
dst = destination 目的

>>> import shutil
# copyfileobj只是了解底层原理,实际代码不需要使用
>>> f1 = open('/bin/ls', 'rb')
>>> f2 = open('/tmp/list4', 'wb')
>>> shutil.copyfileobj(f1, f2)
>>> f1.close()
>>> f2.close()

# shutil.copyfile只拷贝内容
>>> shutil.copyfile('/bin/ls', '/tmp/list5')
# shutil.copy既拷贝内容,又拷贝权限 
>>> shutil.copy('/bin/ls', '/tmp/list6')
# shutil.copy2相当于系统命令cp -p
>>> shutil.copy2('/bin/ls', '/tmp/list7')
# shutil.move => 相当于系统命令mv
>>> shutil.move('/tmp/list7', '/var/tmp/list')
# copytree相当于cp -r
>>> shutil.copytree('/etc/security', '/tmp/security')
>>> shutil.move('/tmp/security', '/var/tmp/auquan')
# rmtree 相当于rm -rf
>>> shutil.rmtree('/var/tmp/auquan')

# 删除单个文件的函数在os模块
>>> import os
>>> os.remove('/tmp/list5')
# 改变属主属组
>>> shutil.chown('/tmp/list', user='bob', group='bob')

权限管理

  • shutil.copymode(src, dst, *, follow_symlinks=Tru
    将权限位从src复制到dst。文件内容,所有者和组不受影响。src和dst是以字符串形式给出的路径名称。
  • shutil.copystat(src, dst, *, follow_symlinks=True)将权限位,最后访问时间,上次修改时间和标志从src复制到dst。

subprocess模块:用于调用系统命令

概述

  • subprocess模块主要用于执行系统命令
  • subprocess模块允许你产生新的进程,连接到它们的输入/输出/错误管道,并获得它们的返回代码
  • 本模块旨在替换几个较早的模块和功能,如os.system、os.spawn*

run方法

  • subprocess.run方法在python3.5引入。早期版本可以使用subprocess.call方法
  • 直接执行命令
>>> subprocess.run(['ls', '/home'])
bob  lisi  Student  wangwu  zhangsan
CompletedProcess(args=['ls', '/home'], returncode=0)

>>> subprocess.run('ls /home', shell=True)
bob  lisi  Student  wangwu  zhangsan
CompletedProcess(args='ls /home', returncode=0)

>>> subprocess.run(['ls', '~'])
ls: 无法访问~: 没有那个文件或目录
CompletedProcess(args=['ls', '~'], returncode=2)

>>> subprocess.run('ls ~', shell=True)
subprocess.run的返回值
import subprocess
rc = subprocess.run('ping -c2 192.168.1.254 &> /dev/null', shell=True)
print(rc.returncode)    # rc.returncode 这就是系统命令$?

输出和错误

  • run方法执行的结果默认打印在屏幕上,也可以通过管道将其存储在标准输出和标准错误中
# 捕获输出
rc = subprocess.run('id root; id bob',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print(rc.stdout)        # 将bytes类型转换成str类型
print(rc.stderr)

python语法风格

变量赋值
  • python支持链式多重赋值
    x = y = 10
  • 另一种将多个变量同时赋值的方法称为多元赋值,采用这种方式赋值时,等号两边的对象都是元组
    a, b = 10, 20
合法标识符
  • python标识符字符串规则和其他大部分用C编写的高级语言相似
  • 第一个字符必须是字母或下划线(_)
  • 剩下的字符可以是字母和数字或下划线
  • 大小写敏感
关键字
  • 和其他的高级语言一样,python也拥有一些被称作关键这字的保留字符
  • 任何语言的关键字应该保持相对的稳定,但是因为python是一门不断成长和进化的语言,其关键字偶尔会更新
  • 关键字列表和iskeyword()函数都放入了keyword模块以便查阅
>>> x = y = 10
>>> a, b = 10, 20
>>> x, y = (100, 200)
>>> m, n = [1, 2]
>>> a, b = b, a      # a和b的值互换
# python的关键字
>>> import keyword
>>> keyword.kwlist
>>> 'pass' in keyword.kwlist
True
>>> keyword.iskeyword('pass')
True

内建函数不是关键字,可以被覆盖,但是最好不覆盖它。

内建
  • 除了关键字之外,python还有可以在任何一级代码使用的“内建”的名字集合,这些名字可以由解释器设置或使用
  • 虽然built-in不是关键字,但是应该把它当作“系统保留字”

内建函数:https://docs.python.org/zh-cn/3/library/functions.html

python模块布局

#!/usr/local/bin/python3
"""
文档字符串,用于帮助"""
import os
# 导入模块
import string
debug = True
# 全局变量
all_chs = string.ascii_letters + string.digits
class MyClass:
# 类的声明
pass
def my_func():
# 函数声明
pass
if __name__ == '__main__':
# 程序主体代码
mc = MyClass()
my_func()

编程思路

  1. 发呆。思考程序运行方式:交互?非交互?
# filename: /etc/hosts
文件已存在,请重试
# filename: /tmp/abc.txt
请输入内容,输入end表示结束
(end to quit)> Hello World!
(end to quit)> the end.
(end to quit)> bye bye.
(end to quit)> end
  1. 分析程序有哪些功能,将功能写成函数,编写出大致的框架
def get_fname():
'用于获取文件名'
pass
def get_content():
'用于获取文件内容,将文件内容以列表形式返回'
pass
def wfile(fname, content):
'将内容与到文件'
pass
  1. 编写程序主体,依次调用各个函数
if __name__ == '__main__':
fname = get_fname()
content = get_content()
wfile(fname, content)
  1. 完成每个函数功能。
模块结构及布局
  • 编写程序时,应该建立一种统一且容易阅读的结构,并将它应用到每一个文件中去
#!/usr/bin/env python3				#起始行
“this	is	a	test	module”		#模块文档字符串
import	sys							#导入模块
import	os
debug	=	True			  		#全局变量声明
class	FooClass(object):			#类定义
'Foo	class'
pass
def test():							#函数定义
"test	function"
foo	=	FooClass()
if	__name__	==	‘__main__’:		#程序主体
test()

字符串详解

序列

序列类型操作符
序列操作符 作用
seq[ind] 获得下标为ind的元素
seq[ind1:ind2] 获得下标从ind1到ind2间的元素集合
seq * expr 序列重复expr次
seq1 + seq2 连接序列seq1和seq2
obj in seq 判断obj元素是否包含在seq中
obj not in seq 判断obj元素是否不包含在seq中
内建函数
函 数 含 义
list(iter) 把可迭代对象转换为列表
str(obj) 把obj对象转换成字符串
tuple(iter) 把一个可迭代对象转换成一个元组对象
  • len(seq):返回seq的长度
  • enumerate:接受一个可迭代对象作为参数,返回一个enumerate对象
  • reversed(seq):接受一个序列作为参数,返回一个以逆序访问的迭代器
  • sorted(iter):接受一个可迭代对象作为参数,返回一个有序的列表
# list用于转换成列表
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> list(range(1, 10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list((10, 20, 30))
[10, 20, 30]

# tuple用于转换成元组
>>> tuple('hello')
('h', 'e', 'l', 'l', 'o')
>>> tuple(range(1, 10))
(1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> tuple(['bob', 'tom', 'jerry'])
('bob', 'tom', 'jerry')

# str用于转成字符串
>>> str(100)
'100'
>>> str([100, 200])
'[100, 200]'

常用于序列对象的方法:

>>> from random import randint
>>> num_list = [randint(1, 100) for i in range(5)]
>>> num_list
[53, 95, 37, 50, 54]

# reversed用于翻转
>>> list(reversed(num_list))
[54, 50, 37, 95, 53]
>>> for i in reversed(num_list):
...     print(i)

# sort排序
>>> sorted(num_list)
[37, 50, 53, 54, 95]
>>> sorted(num_list, reverse=True)   # 降序
[95, 54, 53, 50, 37]

# enumerate返回下标和元素
>>> list(enumerate(num_list))
[(0, 53), (1, 95), (2, 37), (3, 50), (4, 54)]
>>> for data in enumerate(num_list):
...     print(data)
>>> for ind, num in enumerate(num_list):
...     print(ind, num)

字符编码:

ASCII(American Standard Code for Information Interchange,美国信息交换标准代码。
ISO-8859-1(Latin1):欧洲常用的字符编码
中国采用的字符编码:GB2312 / GBK / GB18030
ISO国际标准化组织发布了标准的万国码:unicode。UTF8就是一种实现方式

字符串

字符串操作符
  • 比较操作符:字符串大小按ASCII码值大小进行比较
  • 切片操作符:[ ]、[ : ]、[ : : ]
  • 成员关系操作符:in、not in
格式化操作符
  • 字符串可以使用格式化符号来表示特定含义
格式化字符 转换方式
%c 转换成字符
%s 优先用str()函数进行字符串转换
%d / %i 转成有符号十进制数
%o 转成无符号八进制数
%e / %E 转成科学计数法
%f / %F 转成浮点数
  • 字符串可以使用格式化符号来表示特定含义
辅助指令 作用
* 定义宽度或者小数点精度
- 左对齐
+ 在正数前面显示加号
在正数前面显示空格
# 在八进制数前面显示零0,在十六进制前面显示’0x’或者’0X’
0 显示的数字前面填充0而不是默认的空格
>>> '%s is %s years old' % ('tom', 20)
'tom is 20 years old'
>>> '%s is %d years old' % ('tom', 20)
'tom is 20 years old'
>>> '%10s%8s' % ('name', 'age')   # 第一列宽度为10,第二列8
'      name     age'
>>> '%10s%8s' % ('tom', 20)
'       tom      20'
>>> '%-10s%-8s' % ('name', 'age')
'name      age     '
>>> '%-10s%-8s' % ('tom', 20)
'tom       20      '

>>> '%d' % (5 / 3)
'1'
>>> '%f' % (5 / 3)   # 浮点数
'1.666667'
>>> '%.2f' % (5 / 3)   # 保留两位小数
'1.67'
>>> '%6.2f' % (5 / 3)   # 总宽度为6,小数位2位
'  1.67'
>>> '%#o' % 10   # 8进制
'0o12'
>>> '%#x' % 10    # 16进制
'0xa'
>>> '%e' % 128000   # 科学计数法
'1.280000e+05'
format函数
  • 使用位置参数
    – ‘my name is {} ,age {}’.format(‘hoho’,18)
  • 使用关键字参数
    – ‘my name is {name},age is
    {age}’.format({‘name’:‘bob’, ‘age’:23})
  • 填充与格式化
    – {:[填充字符][对齐方式 <^>][宽度]}
  • 使用索引
    – ‘name is {0[0]} age is {0[1]}’.format([‘bob’, 23])
>>> '{} is {} years old'.format('tom', 20)
'tom is 20 years old'
>>> '{} is {} years old'.format(20, 'tom')
'20 is tom years old'
>>> '{1} is {0} years old'.format(20, 'tom')
'tom is 20 years old'
>>> '{0[1]} is {0[0]} years old'.format([20, 'tom'])
'tom is 20 years old'
>>> '{:<10}{:<8}'.format('tom', 20)  # 左对齐,宽度为10、8
'tom       20      '
>>> '{:>10}{:>8}'.format('tom', 20)   # 右对齐
'       tom      20'
原始字符串操作符
  • 原始字符串操作符是为了对付那些在字符串中出现的特殊字符
  • 在原始字符串里,所有的字符都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符
>>> win_path = 'c:\temp'
>>> print(win_path)   # \t将被认为是tab
c:	emp
>>> win_path = 'c:\\temp'   # \\真正表示一个\
>>> print(win_path)
c:\temp
>>> wpath = r'c:\temp\new'  # 原始字符串,字符串中的字符都表示字面本身含义
>>> print(wpath)
c:\temp\new
>>> wpath
'c:\\temp\\new'

字符串方法

  • string.capitalize():把字符串的第一个字符大写
  • string.center(width):返回一个原字符串居中,并使用空格填充至长度width 的新字符串
  • string.count(str, beg=0,end=len(string)):返回str在string里面出现的次数,如果beg或者end指定则返回指定范围内str出现的次数
  • string.endswith(obj, beg=0,end=len(string)):检查字符串是否以obj结束,如果beg或者end指定则检查指定的范围内是否以obj结束,如果是,返回True,否则返回False
  • string.islower():如果string中包含至少一个区分大小写的字符,并且所有这些字符都是小写,则返回True,否则返回False
  • string.strip():删除string 字符串两端的空白
  • string.upper():转换string 中的小写字母为大写
  • string.split(str="", num=string.count(str)):以str为分隔符切片string,如果num有指定值,则仅分隔num个子字符串
# 去除字符串两端空白字符
>>> ' \thello world!\n'.strip()
'hello world!'
# 去除字符串左边空白字符
>>> ' \thello world!\n'.lstrip()
'hello world!\n'
# 去除字符串右边空白字符
>>> ' \thello world!\n'.rstrip()
' \thello world!'

>>> hi = 'hello world'
>>> hi.upper()   # 将字符串中的小写字母转成大写
'HELLO WORLD'
>>> 'HELLO WORLD'.lower()   # 将字符串中的大写字母转成小写
'hello world'
>>> hi.center(30)   # 居中
'         hello world          '
>>> hi.center(30, '*')
'*********hello world**********'
>>> hi.ljust(30)
'hello world                   '
>>> hi.ljust(30, '#')
'hello world###################'
>>> hi.rjust(30, '@')
'@@@@@@@@@@@@@@@@@@@hello world'
>>> hi.startswith('h')   # 字符串以h开头吗?
True
>>> hi.startswith('he')
True
>>> hi.endswith('o')   # 字符串以o结尾吗?
False
>>> hi.replace('l', 'm')   # 把所有的l替换成m
'hemmo wormd'
>>> hi.replace('ll', 'm')
'hemo world'
>>> hi.split()   # 默认以空格进行切割
['hello', 'world']
>>> 'hello.tar.gz'.split('.')    # 以点作为分隔符切割
['hello', 'tar', 'gz']
>>> str_list = ['hello', 'tar', 'gz']
>>> '.'.join(str_list)   # 以点为分隔符进行字符串拼接
'hello.tar.gz'
>>> '-'.join(str_list)
'hello-tar-gz'
>>> ''.join(str_list)
'hellotargz'
>>> hi.islower()   # 判断字符串内的字母都是小写的吗?
True
>>> 'Hao123'.isupper()   # 字符串内的字母都是大写的吗?
False
>>> 'hao123'.isdigit()   # 所有的字符都是数字字符吗?
False
>>> '123'.isdigit()
True

猜你喜欢

转载自blog.csdn.net/weixin_44965389/article/details/94649461