day19:os模块&shutil模块&tarfile模块
发布时间:2020-12-20 09:56:36 所属栏目:Python 来源:网络整理
导读: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 执行系统命令返回对象 #
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() res = obj.read() print(res) 3.listdir 获取指定文件夹中的所有内容的名称列表 lst = os.listdir(.") . 当前目录 lst = os.listdir(.. . 上一级目录 lst = os.listdir(/home/libolun) (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/myworkmkdir ceshi100 6.environ 获取或修改环境变量 print(os.environ) 获取所有环境变量 print(os.environ[path]) os.environ["] += :/home/libolun/mywork" 修改环境变量 os.system(libolun### os模块属性 #### 1.name 获取系统标识 linux,mac->posix windows->nt (os.name) 2.sep 获取路径分割符号(os.sep) 3.linesep 获取系统的换行符号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( 3.os.mkdir 创建文件夹 os.mkdir(ceshi100 4.os.rmdir 删除文件夹 os.rmdir( 5.os.rename 对文件/文件夹重命名 os.rename(111.txt",123.py 6.os.makedirs 递归创建文件夹 os.makedirs(a/b/c/d/e 7.os.removedirs 递归删除文件夹(空文件夹) os.removedirs(") shutil:负责复制和移动(6+2+1)copyfileobj? copyfile? copymode? copystat? copy? copy2? copytree? rmtree? remove 1.copyfileobj(fsrc,fdst,[,length=16*1024]) 单纯复制文件的内容 需要手动创建文件对象 fp1 = open('r',encoding=utf-8) fp2 = open(lianxi111.phpw) shutil.copyfileobj(fp1,fp2) 2.copyfile(src,dst) 单纯的仅复制文件内容 在底层调用了copyfileobj shutil.copyfile(1.php2.js 3.copymode(src,dst) 单纯的复制权限,不包括文件内容 shutil.copymode( 4.copystat(src,dst) 复制文件所有状态信息,不包括内容 状态信息:包括权限、组、用户、修改时间 shutil.copystat(1.py2.php 5.copy(src,dst) 复制文件的权限和内容 shutil.copy(2.html 6.copy2(src,dst) 复制文件权限和内容,还有组,时间,用户等 shutil.copy2( 7.copytree(src,dst) 复制文件夹里的所有内容(递归拷贝) shutil.copytree(lianxi001lianxi002 8.rmtree(path) 删除当前文件夹以及文件夹中的所有内容(递归删除) shutil.rmtree( 9.move(path1,path2) 移动文件或者文件夹 shutil.move() shutil.move(../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) 1.py 2.dirname 返回路径部分 res = os.path.dirname(strvar) /home/libolun/mywork 3.split 将路径拆分为单独的文件部分和路径部分,并组合成一个元组 tup = os.path.split(strvar) (tup) 4.join 将多个路径和文件组成新的路径 可以自动通过不同的系统加不同的斜杠 path1 = ceshi1 path2 = path3 = 2.py" pathvar = path1 + os.sep + path2 + os.sep + path3 pathvar = os.path.join(path1,path2,path3) (pathvar) 5.splitext 将路径分割为后缀和其他部分 res = os.path.splitext(strvar) ('/home/libolun/mywork/1','.py') 6.getsize 获取文件的大小 res = os.path.getsize( 文件夹不能计算 res = os.path.getsize(os 7.isdir 检测路径是否是一个文件夹 strvar = /home/libolun/mywork/libolun os.path.isdir(strvar) 8.isfile 检测路径是否是一个文件 strvar = os.path.isfile(strvar) 9.islink 检测路径是否是一个链接 strvar = os.path.islink(strvar) 10.getctime [windows]文件的创建时间 [linux]权限的改动时间 返回的是时间戳 res = os.path.getctime( 11.getmtime 获取文件最后一次修改时间 res = os.path.getmtime( 12.getatime 获取文件最后一次访问时间 res = os.path.getatime( 13.exists 检测指定的路径是否存在 strvar = os.path.exists(strvar) 14.isabs 检测一个路径是否是绝对路径 pathvar = os.path.isabs(pathvar) 15.abspath 将相对路径转化为绝对路径 pathnew = os.path.abspath(pathvar) (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) tf.add(bin/echoechobin/ededbin/fuser/tmp/fuser) tf.close() tf = tarfile.open(ceshi002.tar.gzw:gzceshi003.tar.bz2w:bz2) tf.close() 2.对压缩包进行解压 tf = tarfile.open( 解压单个 tf.extract(ceshi004 解压所有 tf.extractall(ceshi005 3.追加文件 支持with语法 只能为没有压缩过的tar包进行追加 with tarfile.open(a) as tf: tf.add(/bin/cpcp 4.查看压缩包中的文件 with tarfile.open() as tf: lst = tf.getnames() print(lst) 思考题:计算一个文件夹的大小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不能再已经压缩过的保重追加内容的问题os path = 找到要解压的包的路径 pathvar1 = os.path.join(path,1)">ceshi0729_3.tar.bz2 解压到哪里去 pathvar2 = os.path.join(path,1)">ceshi0729_3 (1) 先对已经压缩过的包进行解压 with tarfile.open(pathvar1,1)">) 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,) as tf: if i != : 拼接完整路径 pathnew = os.path.join(pathvar2,i) add(路径,别名) tf.add(pathnew,i) ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |