码迷,mamicode.com
首页 > 编程语言 > 详细

python 常用内置模块使用

时间:2019-11-19 01:28:35      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:扩展   类型   mkdir   换行符   ice   conf   extract   ini   basename   

 



python模块分类:1,标准库2,开源模块3,自定义模块

python 常用内置模块使用
1,time与datetime获取时间戳: time.time()  时间元组:  time.localtime()获取格式化字符串: time.strftime("%Y-%m-%d %H:%M:%S")时间格式转换:时间戳-> 时间元组: time.localtime(时间戳),time.gmtime(时间戳)时间戳-> 字符串格式:  time.strftime("%Y-%m-%d %H:%M:%S",时间戳) , time.ctime(时间戳)字符串->元组:  time.strptime("2014-06-02 11:57:02","%Y-%m-%d %H:%M:%S")字符串->时间戳: 元组-> 时间戳: time.mktime(元组)元组->字符串: time.asctime(元组)datetime是time的封装获取当前时间:  datetime.datetime.now()获取5天后时间: datetime.datetime.now() +datetime.timedelta(5)获取5天前时间: datetime.datetime.now() +datetime.timedelta(-5)当前时间加3小时: datetime.datetime.now() +datetime.timedelta(hours=3)当前时间加15分钟: datetime.datetime.now() +datetime.timedelta(minutes=15)2, random模块生成0-1之间的随机数: random.random()随机整数: random.randint(1,5)   #范围[1,5]random.randrange(1,5) # 范围[1,5) 不包含结尾随机浮点数:random.uniform(1,10)序列随机选择: random.choice(‘STRING‘) #序列都可以 string,list,元组random.choice(["a","c","d","e"])从字符串中取特定数量字符: random.sample("teststring",3)洗牌: lists=[1,2,3,4,5,6,7,8]    random.shuffle(lists)  # 序列打散3,os模块获取当前工作目录:os.getcwd()  # pwd切换目录: os.chdir() 返回当前目录:os.curdir获取父目录路径: os.pardir递归创建目录: os.makedirs()递归移除目录:os.removedirs()创建目录:os.mkdir()删除目录:os.rmdir()列出目录下的文件:os.listdir()删除文件:os.remove()改名: os.rename()执行一个文件:os.stat()系统特定路径分隔符 : os.sep换行符:os.linesep用于分隔文件路径的字符串:os.pathsep系统平台字符串:os.name执行系统命令:os.system获取系统环境变量:os.evniron获取绝对路径:os.path.abspath(path)目录和文件分隔:os.path.split(path)获取路径的目录:os.path.dirname(path)获取路径的文件:os.path.basename(path)判断文件是否存在:os.path.exists(path)判断是否绝对路径:os.path.isabs(path)判断是否是文件:os.path.isfile(path)判断是否是目录:os.path.isdir(path)路径组合:os.path.join(path1,path2....)获取文件最后存取时间:os.path.getatime(path)获取文件最后修改时间:os.path.getmtime(path)3,sys模块sys.argv: 命令行参数列表,第一个元素为程序路径sys.exit(n): 退出程序sys.version: python解释器版本sys.path:  返回系统PATHsys.platform: 系统平台sys.stdout.write("mssage") : 打印字符sys.stdin.readline() : 获取输入数据4,shutil模块shutil.copyfile(src,dst) : 拷贝文件shutil.coypmode(src,dst) : 仅拷贝权限。内容,组,用户信息不变。shutil.copystat(src,dst) : 拷贝状态信息: mode,atime,mtime,flagsshutil.copy(src,dst) : 拷贝文件和权限shutil.copy2(src,dst): 拷贝文件和状态信息shutil.copytree(src,dst,symlinks=False,ignore=None):递归拷贝文件shutil.rmtree(path) : 递归删除文件shutil.move(src,dst): 递归的移动文件shutil.make_archive(base_name,format,root_path,owner,group,logger):创建压缩文件并返回文件路径base_name:压缩包路径format:压缩类型:zip,tar,bztar,gztarroot_path:要压缩的文件夹路径,默认当前目录owner:用户,默认当前用户group:组,默认当前组logger:日志对象扩展:用zipfile压缩文件:import zipfilezfile = zipfile.ZipFile("target.zip","w")z.write(file1)z.write(file2)z.close()zipfile解压文件:zfile.zipfile.ZipFile("target.zip","r")zfile.extractall()z.close()5,xml模块import xml.etree.ElementTree as ET6, yaml模块import yaml7, ConfigParser 模块ini配置文件import configparser8,hashlib模块用于替代md5和sha模块,主要提供sha1,sha224,sha256,sha384,sha512,md5加密import hashlibm = hashlib.md5() #创建一个MD5对象m.update(b"teststring测试字符串".encode(encoding="utf-8"))print(m.hexdigest())m = hashlib.sha1() #创建一个sha1对象m.update(b"teststring")print(m.hexdigest())import hmach=hmac.new("测试字符串".encode(encoding="utf-8"))h.hexdigest()9,re模块import reres = re.match(模式,字符串)  #匹配到就有返回,否则返回Noneprint(res.group())  # 打印匹配到的内容re.search(模式,字符串): 匹配包含模式的内容re.findall(模式,字符串): 返回所有匹配到的数据列表re.splitall : 将匹配到的字符作为列表分隔符re.sub :匹配字符并替换。

python 常用内置模块使用

标签:扩展   类型   mkdir   换行符   ice   conf   extract   ini   basename   

原文地址:https://www.cnblogs.com/heyong45/p/11886392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!