python3有哪些内置模块(Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析)
python3有哪些内置模块
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析本文实例讲述了python3.5内置模块之os模块、sys模块、shutil模块用法。分享给大家供大家参考,具体如下:
1、os模块:提供对操作系统进行调用的接口
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu import os print (os.getcwd()) #获取当前的操作目录,即当前python脚本工作的目录路径 #os.chdir("f:\\pythoncode\\day5\\test") #改变当前脚本工作目录,相当于shell下的cd os.chdir(r "f:\pythoncode\day5\test" ) #与上面一句等价(推荐使用) print (os.getcwd()) print (os.curdir) #返回当前目录 '.' print (os.pardir) #获取当前目录的父目录字符串名 '..' os.makedirs(r "f:\a\b\c" ) #生成多层递归目录 #若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.removedirs(r "f:\a\b\c" ) #清理空文件夹 os.mkdir(r "f:\pythoncode\day5\t" ) #生成单级目录,相当于shell中mkdir filename os.rmdir(r "f:\pythoncode\day5\t" ) #删除单级空目录,若目录不为空,无法删除或报错;相当于shell中rmdir filename print (os.listdir(r "f:\pythoncode\day5\test" )) #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove(r "f:\pythoncode\day5\test\1.py" ) #删除一个文件 os.rename(r "f:\pythoncode\day5\test\1.py" ,r "f:\pythoncode\day5\test\2.py" ) #重命名文件/目录 print (os.stat(r "f:\pythoncode\day5\test" )) #获取文件/目录信息 print (os.sep) #输出操作系统特定的路径分隔符,win下为"\\",linux下为"/" print (os.linesep) #输出当前平台使用的行终止符,win下为"\r\n",linux下为"\n" print (os.pathsep) #输出用于分割文件路径的字符串,win下为";",linux下为":" print (os.environ) #查看系统的环境变量 print (os.name) #输出字符串指示当前使用平台。win->'nt'; linux->'posix' print (os.system( "dir" )) #运行shell命令,直接显示 print (os.path.abspath(r "f:\pythoncode\day5" )) #返回path规范化的绝对路径 print (os.path.split(r "f:\pythoncode\day5\test\1.py" )) #将path分割成目录和文件名二元组返回 print (os.path.dirname(r "f:\pythoncode\day5" )) #返回path的目录。其实就是os.path.split(path)的第一个元素 print (os.path.basename(r "f:\pythoncode\day5" )) #返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。 print (os.path.exists(r "f:\pythoncode\day5" )) #如果path存在,返回true;如果path不存在,返回false print (os.path.isabs(r "f:\pythoncode\day5" )) #如果path是绝对路径,返回true print (os.path.isfile(r "f:\pythoncode\day5\p_test.py" )) #如果path是一个存在的文件,返回true,否则返回false print (os.path.isdir(r "f:\pythoncode\day5" )) #如果path是一个存在的目录,则返回true。否则返回false print (os.path.join(r "f:" ,r "\pythoncode" ,r "\day5" ,r "\day" )) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 print (os.path.getatime(r "f:\pythoncode\day5" )) #返回path所指向的文件或者目录的最后存取时间 print (os.path.getmtime(r "f:\pythoncode\day5" )) #返回path所指向的文件或者目录的最后修改时间 |
运行结果:
|
f:\pythoncode\day5 f:\pythoncode\day5\test . .. [ '2.py' , 'module_test.py' , 'test.py' ] os.stat_result(st_mode = 16895 , st_ino = 8162774324625191 , st_dev = 104211 , st_nlink = 1 , st_uid = 0 , st_gid = 0 , st_size = 4096 , st_atime = 1506609480 , st_mtime = 1506609480 , st_ctime = 1506579769 ) \ ; environ({ 'processor_level' : '6' , 'windows_tracing_logfile' : 'c:\\bvtbin\\tests\\installpackage\\csilogfile.log' , 'processor_architecture' : 'x86' ) nt ������ f �еľ��� ѧϰ�� ������к��� 0001 - 9713 f:\pythoncode\day5\test ��Ŀ¼ 2017 / 09 / 28 22 : 38 < dir > . 2017 / 09 / 28 22 : 38 < dir > .. 2017 / 09 / 28 22 : 37 69 2.py 2017 / 09 / 28 14 : 31 121 module_test.py 2017 / 09 / 28 14 : 35 237 test.py 3 ���ļ� 427 �ֽ� 2 ��Ŀ¼ 14 , 051 , 733 , 504 �����ֽ� 0 f:\pythoncode\day5 ( 'f:\\pythoncode\\day5\\test' , '1.py' ) f:\pythoncode day5 true true true true f:\day 1506656912.210523 1506656912.210523 |
2、sys模块
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu import sys print (sys.argv) #命令行参数list,第一个元素是程序本身路径 print (sys.version) #获取python解释程序的版本信息 print (sys.path) #返回模块的搜索路径,初始化时使用pythonpath环境变量的值 print (sys.platform) #返回操作系统平台名称 sys.stdout.write( 'please:' ) #标准输出,写入字符串输出到屏幕 val = sys.stdin.readline()[: - 1 ] #标准输入 print (val) sys.exit( 0 ) #退出程序,正常退出时exit(0) |
运行结果:
|
[ 'f:/pythoncode/day5/sys_module.py' ] 3.5 . 2 |anaconda 4.2 . 0 ( 32 - bit)| (default, jul 5 2016 , 11 : 45 : 57 ) [msc v. 1900 32 bit (intel)] [ 'f:\\pythoncode\\day5' , 'f:\\pythoncode' , 'c:\\users\\administrator\\anaconda3\\python35.zip' , 'c:\\users\\administrator\\anaconda3\\dlls' , 'c:\\users\\administrator\\anaconda3\\lib' , 'c:\\users\\administrator\\anaconda3' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages\\sphinx-1.4.6-py3.5.egg' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages\\win32' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages\\win32\\lib' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages\\pythonwin' , 'c:\\users\\administrator\\anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.5.egg' ] win32 hello please:hello |
3、shutil模块:高级的文件、文件夹、压缩包处理模块
(1)将文件内容拷贝到另一个文件中,可以部分内容——shutil.copyfileobj(fsrc, fdst[, length])
|
f1 = open ( "p_test.py" ,encoding = "utf-8" ) f2 = open ( "p1.py" , "w" ,encoding = "utf-8" ) #将文件p_test.py内容拷贝到另一个文件p1.py中,可以部分内容 shutil.copyfileobj(f1,f2) |
运行结果:
(2)拷贝文件——shutil.copyfile(src, dst)
|
import shutil shutil.copyfile( "p1.py" , "p2.py" ) #拷贝文件 |
运行结果:
(3)仅拷贝权限(内容、组、用户均不变)——shutil.copymode(src, dst)
(4)拷贝状态的信息,包括:mode bits, atime, mtime, flags——shutil.copystat(src, dst)
(5)拷贝文件和权限——shutil.copy(src, dst)
(6)拷贝文件和状态信息——shutil.copy2(src, dst)
(7)递归的去拷贝文件:
|
shutil.ignore_patterns( * patterns) shutil.copytree(src, dst, symlinks = false, ignore = none) |
|
import shutil shutil.copytree( "test" , "test1" ) #递归的去拷贝文件 |
运行结果:
(8)递归的去删除文件——shutil.rmtree(path[, ignore_errors[, onerror]])
(9)递归的去移动文件——shutil.move(src, dst)
(10)创建压缩包并返回文件路径,例如:zip、tar——shutil.make_archive(base_name, format,...)
-
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/users/wupeiqi/www =>保存至/users/wupeiqi/ - format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要压缩的文件夹路径(默认当前目录)
- owner: 用户,默认当前用户
- group: 组,默认当前组
- logger: 用于记录日志,通常是logging.logger对象
|
import shutil shutil.make_archive( "shutil_archive_test" , "zip" , "f:\pythoncode\day5" ) |
运行结果:
总结:shutil 对压缩包的处理是调用 zipfile 和 tarfile 两个模块来进行的
|
import zipfile z = zipfile.zipfile( "day5.zip" , "w" ) z.write( "p1.py" ) print ( "===========" ) z.write( "p2.py" ) z.close() |
运行结果:
|
= = = = = = = = = = = |
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/loveliuzz/article/details/78128080
- python如何将运行结果存入txt中(详解python读取和输出到txt)
- python解析视频源码(基于python实现高速视频传输程序)
- python怎么装opencv(Python基于opencv实现的简单画板功能示例)
- python 装饰器模式(python重试装饰器的简单实现方法)
- python批量注册(python实现批量注册网站用户的示例)
- python中pip和pip3有什么区别(ISAPI-REWRITE伪静态规则写法以及说明)
- 能自动点赞的小程序(python实现QQ空间自动点赞功能)
- python的基本函数及用法(Python3.6.x中内置函数总结及讲解)
- python使用aes加密解密(python实现AES加密与解密)
- python调用支付宝支付接口(python实现支付宝转账接口)
- 用mysql语句写python学生管理系统(Python基于mysql实现学生管理系统)
- python多线程超时设置(解决python线程卡死的问题)
- python零基础入门详细教程(Python零基础入门学习之输入与输出)
- python中的数据类型和数字类型(Python数据类型之Number数字操作实例详解)
- python常用的属性和方法(Python进阶之@property动态属性的实现)
- 用python画圣诞树送给女朋友(情人节快乐! python绘制漂亮玫瑰花)
- 职场人改不掉这4个习惯,只会越混越穷,一辈子也翻不了身(职场人改不掉这4个习惯)
- 华为 联想等46家公司笔试面试题,涉及各行各业,建议收藏(联想等46家公司笔试面试题)
- ()
- ()
- 800壮士拼死拖住30万日军 八佰 的真实历史,誓与阵地共存亡(800壮士拼死拖住30万日军)
- 演员陈创,火于 哮天犬 ,颠峰于 福贵 ,现状却令人唏嘘(演员陈创火于哮天犬)
热门推荐
- css3行内样式表图解(详解css3自定义滚动条样式写法)
- mysql有什么索引(MySQL 普通索引和唯一索引的区别详解)
- dedecms文章权重排序(修改dedecms文章标题长度限制的方法)
- yii2和laravel框架哪个比较简单(关于Yii中模型场景的一些简单介绍)
- 云服务器如何选择(好的云服务器需要怎么选择?)
- python高级面试题及答案(python面试题小结附答案实例代码)
- docker停止删除所有容器方法(Docker stop停止/remove删除所有容器)
- css如何去掉本身的浮动元素(CSS清除浮动的常用方法优缺点分析)
- 阿里云rds移动数据库(阿里云跨区域服务器迁移镜像迁移,RDS数据库迁移图文教程)
- 用css实现图片特效代码(HTML+CSS+JavaScript实现图片3D展览的示例代码)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9