day19:os模块&shutil模块&tarfile模块
os模块:对系统进行操作(6+3)
system popen listdir getcwd chdir environ / name sep linesep
import os #### os模块方法 #### # 1.system 在python中执行系统命令
os.system("ifconfig")
os.system("touch 1.txt") # 2.popen 执行系统命令返回对象
# 可以通过read方法读出字符串,防止字符串乱码,使用popen进行操作
obj = os.popen("ifconfig")
res = obj.read()
print(res) # 3.listdir 获取指定文件夹中的所有内容的名称列表
lst = os.listdir(".") # . 当前目录
lst = os.listdir("..") # . 上一级目录
lst = os.listdir("/home/libolun")
print(lst) # 4.getcwd 获取当前文件所在的默认路径(单纯的路径)
res = os.getcwd()
print(res) # /mnt/hgfs/python31_gx/day19
# 路径+文件
print(__file__) # /mnt/hgfs/python31_gx/day19/1.py # 5.chdir 修改当前文件工作的默认路径
os.chdir("/home/libolun/mywork")
os.system("mkdir ceshi100") # 6.environ 获取或修改环境变量
print(os.environ) # 获取所有环境变量
print(os.environ["path"])
os.environ["path"] += ":/home/libolun/mywork" # 修改环境变量
os.system("libolun") #### os模块属性 #### # 1.name 获取系统标识
# linux,mac->posix windows->nt
print(os.name) # 2.sep 获取路径分割符号
# linux,mac-> / windows-> \
print(os.sep) # 3.linesep 获取系统的换行符号
# linux,max-> \n windows->\r\n或\n
print(repr(os.linesep))
os模块:新建和删除(7)
mknod remove mkdir rmdir rename makedirs removedirs
#### os 负责新建和删除 #### # 1 os.mknod 创建文件
os.mknod("1.txt") # 2.os.remove 删除文件
os.remove("1.txt") # 3.os.mkdir 创建文件夹
os.mkdir("ceshi100") # 4.os.rmdir 删除文件夹
os.rmdir("ceshi100") # 5.os.rename 对文件/文件夹重命名
os.rename("111.txt","123.py") # 6.os.makedirs 递归创建文件夹
os.makedirs("a/b/c/d/e") # 7.os.removedirs 递归删除文件夹(空文件夹)
os.removedirs("a/b/c/d/e")
shutil:负责复制和移动(6+2+1)
copyfileobj copyfile copymode copystat copy copy2 copytree rmtree remove
# 1.copyfileobj(fsrc,fdst,[,length=16*1024]) 单纯复制文件的内容
# 需要手动创建文件对象
fp1 = open("123.py",mode='r',encoding="utf-8")
fp2 = open("lianxi111.php",mode="w",encoding="utf-8")
shutil.copyfileobj(fp1,fp2) # 2.copyfile(src,dst) 单纯的仅复制文件内容
# 在底层调用了copyfileobj
shutil.copyfile("1.php","2.js") # 3.copymode(src,dst) 单纯的复制权限,不包括文件内容
shutil.copymode("1.php","2.js") # 4.copystat(src,dst) 复制文件所有状态信息,不包括内容
# 状态信息:包括权限、组、用户、修改时间
shutil.copystat("1.py","2.php") # 5.copy(src,dst) 复制文件的权限和内容
shutil.copy("1.py","2.html") # 6.copy2(src,dst) 复制文件权限和内容,还有组,时间,用户等
shutil.copy2("1.py","2.html") # 7.copytree(src,dst) 复制文件夹里的所有内容(递归拷贝)
shutil.copytree("lianxi001","lianxi002") # 8.rmtree(path) 删除当前文件夹以及文件夹中的所有内容(递归删除)
shutil.rmtree("lianxi001") # 9.move(path1,path2) 移动文件或者文件夹
shutil.move("1.php","..")
shutil.move("lianxi001","../lianxi007") # 移动并改名
os.path:路径模块(6+3+3+3)
basename dirname split join splitext getsize isdir isfile islink getctime getatime getmtime exists isabs abspath
# 1.basename 返回文件名部分
strvar = "/home/libolun/mywork/1.py"
res = os.path.basename(strvar)
print(res) # 1.py # 2.dirname 返回路径部分
res = os.path.dirname(strvar)
print(res) # /home/libolun/mywork # 3.split 将路径拆分为单独的文件部分和路径部分,并组合成一个元组
tup = os.path.split(strvar)
print(tup) # 4.join 将多个路径和文件组成新的路径
# 可以自动通过不同的系统加不同的斜杠
path1 = "ceshi1"
path2 = "libolun"
path3 = "2.py"
# pathvar = path1 + os.sep + path2 + os.sep + path3
pathvar = os.path.join(path1,path2,path3)
print(pathvar) # 5.splitext 将路径分割为后缀和其他部分
res = os.path.splitext(strvar)
print(res) # ('/home/libolun/mywork/1','.py') # 6.getsize 获取文件的大小
res = os.path.getsize("1.py")
# 文件夹不能计算
res = os.path.getsize("os")
print(res) # 7.isdir 检测路径是否是一个文件夹
strvar = "/home/libolun/mywork/libolun"
res = os.path.isdir(strvar)
print(res) # 8.isfile 检测路径是否是一个文件
strvar = "/home/libolun/mywork/libolun"
res = os.path.isfile(strvar)
print(res) # 9.islink 检测路径是否是一个链接
strvar = "/home/libolun/mywork/libolun"
res = os.path.islink(strvar)
print(res) # 10.getctime [windows]文件的创建时间 [linux]权限的改动时间
# 返回的是时间戳
res = os.path.getctime("1.py") # 11.getmtime 获取文件最后一次修改时间
res = os.path.getmtime("1.py") # 12.getatime 获取文件最后一次访问时间
res = os.path.getatime("1.py") # 13.exists 检测指定的路径是否存在
strvar = "/home/libolun/mywork/libolun"
res = os.path.exists(strvar)
print(res) # 14.isabs 检测一个路径是否是绝对路径
pathvar = "."
res = os.path.isabs(pathvar)
print(res) # 15.abspath 将相对路径转化为绝对路径
pathnew = os.path.abspath(pathvar)
print(pathnew) # 检测是否是绝对路径,如果不是,将其转化为绝对路径
if not os.path.abspath(pathvar):
pathnew = os.path.abspath(pathvar)
print(pathnew)
tarfile:压缩模块(4)
压缩 解压 追加 查看
# 1.创建tar包
'''最小的压缩包,后缀格式为bz2'''
# 单纯的tar包
tf = tarfile.open("ceshi001.tar",'w',encoding="utf-8")
tf.add("bin/echo","echo")
tf.add("bin/ed","ed")
tf.add("bin/fuser","/tmp/fuser")
tf.close() tf = tarfile.open("ceshi002.tar.gz",'w:gz',encoding="utf-8")
tf.add("bin/echo","echo")
tf.add("bin/ed","ed")
tf.add("bin/fuser","/tmp/fuser")
tf.close() tf = tarfile.open("ceshi003.tar.bz2",'w:bz2',encoding="utf-8")
tf.add("bin/echo","echo")
tf.add("bin/ed","ed")
tf.add("bin/fuser","/tmp/fuser")
tf.close() # 2.对压缩包进行解压
tf = tarfile.open("ceshi003.tar.bz2",'r',encoding="utf-8")
# 解压单个
tf.extract("echo","ceshi004")
# 解压所有
tf.extractall("ceshi005") # 3.追加文件
# 支持with语法
# 只能为没有压缩过的tar包进行追加
with tarfile.open("ceshi001.tar",'a',encoding="utf-8") as tf:
tf.add("/bin/cp","cp") # 4.查看压缩包中的文件
with tarfile.open("ceshi002.tar.gz",'a',encoding="utf-8") as tf:
lst = tf.getnames()
print(lst)
思考题:计算一个文件夹的大小
import os pathvar = "/mnt/hgfs/python31_gx/day19/ceshi100" # 递归计算文件夹的大小
def getallsize(pathvar):
size = 0
# 获取当前文件夹中所有内容
lst = os.listdir(pathvar)
# 循环列表
for i in lst:
# 拼接完整路径
pathnew = os.path.join(pathvar,i)
# 判断是否是文件或者文件夹
if os.path.isfile(pathnew):
size += os.path.getsize(pathnew)
elif os.path.isdir(pathnew):
# 如果是文件夹,继续调用函数.
size += getallsize(pathnew)
# 返回最后的结果
return size res = getallsize(pathvar)
print(res)
思考题:如何处理tarfile不能再已经压缩过的保重追加内容的问题
import os
path = os.getcwd()
# 找到要解压的包的路径
pathvar1 = os.path.join(path,"ceshi0729_3.tar.bz2")
# 解压到哪里去
pathvar2 = os.path.join(path,"ceshi0729_3") # (1) 先对已经压缩过的包进行解压
with tarfile.open(pathvar1,"r",encoding="utf-8") as tf:
tf.extractall(pathvar2) # (2) 往这个解压的文件夹中添加新的文件
mybin = "cp -a /bin/fgrep " + pathvar2
# print(mybin) # cp -a /bin/fgrep /mnt/hgfs/python31_gx/day19/ceshi0729_3
os.system(mybin) # (3) 对这个文件进行过滤筛选,重新打包压缩 (不要echo)
lst = os.listdir(pathvar2)
with tarfile.open(pathvar1,"w:bz2",encoding="utf-8") as tf:
for i in lst:
if i != "echo":
# 拼接完整路径
pathnew = os.path.join(pathvar2,i)
# add(路径,别名)
tf.add(pathnew,i)
day19:os模块&shutil模块&tarfile模块的更多相关文章
- 模块 - random/string/os/sys/shutil/zipfile/tarfile
random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...
- Python3学习之路~5.6 shutil & zipfile & tarfile模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])#将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(s ...
- Python的os,shutil和sys模块
*********OS*********** os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'n ...
- time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结
time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...
- Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
- 【转】Python之文件与目录操作(os、zipfile、tarfile、shutil)
[转]Python之文件与目录操作(os.zipfile.tarfile.shutil) Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读 ...
- Python之文件与目录操作(os、zipfile、tarfile、shutil)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Python第二十天 shutil 模块 zipfile tarfile 模块
Python第二十天 shutil 模块 zipfile tarfile 模块 os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...
随机推荐
- QT Creator配置环境和安装
原文链接:https://blog.csdn.net/qq_33154343/java/article/details/78587699 补充下其中缺少的步骤:安装后缺少QT GUI Applicat ...
- 切忌一步到位,谈谈DevOps实施落地
2020年6月19日,由云计算开源产业联盟指导,高效运维社区和 DevOps 时代社区联合举办的GNSEC 2020线上峰会圆满举办.BoCloud博云参加了本次峰会并分享了博云帮助客户实施DevOp ...
- Docker数据卷的介绍和使用
最近在学习docker,这篇主要讲了数据卷的作用以及使用,我用的是mac系统去操作的 1.数据卷的简介 2.数据卷的配置 (1).查看你的镜像docker images (2)运行的命令 ~$ doc ...
- laravel7使用auth进行用户认证
原文地址:https://www.wjcms.net/archives/laravel7使用auth进行用户认证 laravel7 版本移除了 auth,大家都知道以前版本是直接使用 php arti ...
- 基于tcp/udp协议的套接字通信
目录 一.套接字分类 二.套接字的工作流程 三.基于tcp协议的套接字 四.基于udp协议的套接字 一.套接字分类 1.基于文件类型的套接字家族:AF_UNIX 2.基于网络类型的套接字家族:AF_I ...
- 数据可视化之powerBI基础(十九)学会使用Power BI的参数,轻松搞定动态分析
https://zhuanlan.zhihu.com/p/55295072 静态的分析经常不能满足实际分析的需要,还需要引入动态分析,通过调节某个维度的增减变化来观察对分析结果的影响.在PowerBI ...
- 创建MongoDB副本集教程
今天有时间搞了一下mongoDB的副本集,遇到好多坑,写下此文,方便日后查阅! 本教程是在windows环境下安装测试的(我是本机一台 + 两台虚拟机) 本机:10.53.8.159 虚拟机一:10. ...
- Arcgis api for js实现服务端地图的增删改查
< !DOCTYPE html > <html xmlns = "http://www.w3.org/1999/xhtml" > <head > ...
- Bash 脚本编程
概述 Bash (GNU Bourne-Again Shell) 是许多Linux发行版的默认Shell. shell语法 变量 定义:your_name="hellohhy" 使 ...
- PJzhang:python基础入门的7个疗程-seven
猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课 https://www.liaoxuefeng.com/wiki/1016959663602400 第19天:开源模块 数据 ...