day6 常用标准模块

一、os

用于提供系统级别的操作

os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat('path/filename')  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"	
",Linux下为"
"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.environ  获取系统环境变量
os.path.abspath(path)  返回path规范化的绝对路径
os.path.split(path)  将path分割成目录和文件名二元组返回
os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是绝对路径,返回True
os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

二、sys

用于提供对解释器相关的操作

sys.argv           命令行参数List,第一个元素是程序本身路径
sys.exit(n)        退出程序,正常退出时exit(0)
sys.version        获取Python解释程序的版本信息
sys.maxint         最大的Int值
sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform       返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]

三、time

时间相关的操作,时间有三种表示方式:

  • 时间戳               1970年1月1日之后的秒,即:time.time()
  • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
  • 结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()
import  time
#时间戳转字符串格式
a = time.time()
print(a)  #打印时间戳
b = time.localtime(a)   #把时间戳转换成时间对象  元组的形式
print(b)
c = time.strftime("%Y-%m-%d %H:%M:%S",b)    #格式化时间  把事件对象转化成格式化的字符串
print(c)
#字符串时间转化为时间戳
d = time.strptime("2016-11-14 09:37:26","%Y-%m-%d %H:%M:%S")
print(d)
e = time.mktime(d)
print(e)
#时间加减
import datetime
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换

random

随机数

import  random
import string
print( random.randint(1,2))  #包含1和2
print(random.randrange(1,3))  #1和2 会出现,3不会出现

#随机生成验证码或密码 str_source = string.ascii_letters + string.digits suji = random.sample(str_source,8) print(suji) print("".join(suji))

随机验证码实例:

import random
checkcode = ''
for i in range(4):
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print checkcode

五、shutil

高级的 文件、文件夹、压缩包 处理模块

shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中,可以部分内容

f = open("example.log")

f2 = open("example_new.log","w")

shutil.copyfileobj(f,f2)

shutil.copyfile(src, dst)
拷贝文件

shutil.copyfile("example_new.log","example_new2.log")

shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变

shutil.copymode("example_new.log","example_new2.log")

shutil.copystat(src, dst)
拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copystat("example_new.log","example_new2.log")


shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件,并且过滤文件夹和文件

shutil.copytree("C:\UsersAdministrators16day5", "C:\UsersAdministrators16day6Test",
                 ignore=shutil.ignore_patterns('atm', '*.log'))

shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

shutil.rmtree("C:\UsersAdministrators16day6Test")

shutil.move(src, dst)
递归的去移动文件

shutil.rmtree("C:\UsersAdministrators16day6Test")

shutil.make_archive(base_name, format,...)

创建压缩包并返回文件路径,例如:zip、tar

hutil.make_archive("day5","zip","C:\UsersAdministrators16day5")

打包和解压缩包

打包:

import zipfile


z = zipfile.ZipFile('ziptest3.zip', 'w')
z.write("C:\UsersAdministrators16day5\access.log.2",arcname="access.log.2")
z.write("C:\UsersAdministrators16day5\access.log.1",arcname="access.log.1")
z.write("example_new.log")
z.write("day5.zip")

z.close()

解压:

import zipfile

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

六、执行系统命令

执行系统命令 

可以执行shell命令的相关模块和函数有:

  • os.system
  • os.spawn*
  • os.popen*          --废弃
  • popen2.*           --废弃
  • commands.*      --废弃,3.x中被移除

call 

执行命令,返回状态码

ret = subprocess.call(["ls", "-l"], shell=False)
ret = subprocess.call("ls -l", shell=True)

shell = True ,允许 shell 命令是字符串形式

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异常

subprocess.check_call(["ls", "-l"])
subprocess.check_call("exit 1", shell=True)

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常

subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)

subprocess.Popen(...)

用于执行复杂的系统命令

参数:

    • args:shell命令,可以是字符串或者序列类型(如:list,元组)
    • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
    • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
      所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    • shell:同上
    • cwd:用于设置子进程的当前目录
    • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    • universal_newlines:不同系统的换行符不同,True -> 同意使用
    • startupinfo与createionflags只在windows下有效
      将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

执行普通命令:

import subprocess
ret1 = subprocess.Popen(["mkdir","t1"])
ret2 = subprocess.Popen("mkdir t2", shell=True)

七、subprocess

执行外部命令

subprocess.run("df")   ###执行命令
subprocess.run(["df", "-h"])   ###执行带参数命令
subprocess.run("df -h | grep /dev/sda3", shell=True)   ###执行shell命令,只有返回状态
call()   ###执行命令,返回状态0 or 非0
check_call()   ###执行命令,如果结果为0返回,否则抛出异常

###接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果
subprocess.getstatusoutput("df -h | grep /dev/sda3")
(0, '/dev/sda3       519G  3.2G  489G   1% /')

###接收字符串格式命令,并返回结果
subprocess.getoutput("df -h | grep /dev/sda3")
'/dev/sda3       519G  3.2G  489G   1% /'

###执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
res = subprocess.check_output("df -h | grep /dev/sda3", shell=True)
>>> res
b'/dev/sda3       519G  3.2G  489G   1% /
'



###上面那些方法,底层都是封装的subprocess.Popen
p = subprocess.Popen("df -h | grep /dev/sda3", shell=True, stdout=subprocess.PIPE)
p.stdout.read()
b'/dev/sda3       519G  3.2G  489G   1% /
'


###检查子进程是否已终止。返回returnCode
poll()

###等待子进程终止。返回returnCode属性
wait()

terminate() 杀掉所启动进程
communicate() 等待任务结束


stdin 标准输入

stdout 标准输出

stderr 标准错误


可用参数:

args:shell命令,可以是字符串或者序列类型(如:list,元组)
bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
shell:同上
cwd:用于设置子进程的当前目录
env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
universal_newlines:不同系统的换行符不同,True -> 同意使用 

startupinfo与createionflags只在windows下有效
将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

八、hashlib

用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

import hashlib
 
# ######## md5 ########
 
hash = hashlib.md5()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha1 ########
 
hash = hashlib.sha1()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash.update('admin')
print hash.hexdigest()
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash.update('admin')
print hash.hexdigest()

以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密

九、json 和 pickle 

用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换
  • pickle,用于python特有的类型 和 python的数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

十、configparser

 对特定配置文件进行操作

# 注释1
; 注释2
 
[section1]
k1 = v1
k2:v2
 
[section2]
k1 = v1
import ConfigParser
 
config = ConfigParser.ConfigParser()
config.read('i.cfg')
 
# ########## 读 ##########
#secs = config.sections()
#print secs
#options = config.options('group2')
#print options
 
#item_list = config.items('group2')
#print item_list
 
#val = config.get('group1','key')
#val = config.getint('group1','key')
 
# ########## 改写 ##########
#sec = config.remove_section('group1')
#config.write(open('i.cfg', "w"))
 
#sec = config.has_section('wupeiqi')
#sec = config.add_section('wupeiqi')
#config.write(open('i.cfg', "w"))
 
 
#config.set('group2','k1',11111)
#config.write(open('i.cfg', "w"))
 
#config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))

十一、logging

用于便捷记录日志且线程安全的模块

import logging
 
 
logging.basicConfig(filename='log.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)
 
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
logging.log(10,'log')

对于等级:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

只有大于当前日志等级的操作才会被记录。

十二、re 

 re模块用于对python的正则表达式的操作

字符:

  . 匹配除换行符以外的任意字符
  w 匹配字母或数字或下划线或汉字
  s 匹配任意的空白符
  d 匹配数字
   匹配单词的开始或结束
  ^ 匹配字符串的开始
  $ 匹配字符串的结束

次数:

  * 重复零次或更多次
  + 重复一次或更多次
  ? 重复零次或一次
  {n} 重复n次
  {n,} 重复n次或更多次
  {n,m} 重复n到m次

IP:
^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$
手机号:
^1[3|4|5|8][0-9]d{8}$

1、match(pattern, string, flags=0)

从起始位置开始根据模型去字符串中匹配指定内容,匹配单个

  • 正则表达式
  • 要匹配的字符串
  • 标志位,用于控制正则表达式的匹配方式
re.match("^a", "alex li")
re.match("^a.", "alex li")

2、search(pattern, string, flags=0)

根据模型去字符串中匹配指定内容,匹配单个

re.search("i$", "alex li")
re.search("a*", "aaaalex li")

3、findall(pattern, string, flags=0)

上述两中方式均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。

re.findall("[a-zA-Z]+", "130435sd-fsd_f19900213x")

4、sub(pattern, repl, string, count=0, flags=0)

用于替换匹配的字符串

re.sub("d{4}", "1995", "i was born in 1982-05-20")
#替换全部
re.sub("d{4}", "1995", "i was born in 1982-05-20 jiafei is in 1920")

5、split(pattern, string, maxsplit=0, flags=0)

根据指定匹配进行分组

#根据.进行分组
re.split("W+", "192.168.1.55")

最常用的匹配语法:

re.match 从头开始匹配(一般不用)
re.search 匹配包含
re.findall 把所有匹配到的字符放到以列表中的元素返回
re.splitall 以匹配到的字符当做列表分隔符
re.sub 匹配字符并替换

原文地址:https://www.cnblogs.com/liyongshan/p/8558390.html