第十三集:系统接口模块(os-zipfile-shutill)

import os
ROOT = os.path.dirname(os.path.abspath(__file__))
print('Root路径',ROOT)
res = os.getcwd() # 获取当前工作目录
print('当前的工作目录', res)
# os.chdir('D:\\2021git\\zxy')
res = os.getcwd()
print(res)
list_dir = os.listdir() # 打印当前工作目录下的所有子目录合文件
print('当前工作空间下所有的子目录和文件', list_dir)

# os.mkdir('abc') #创建目录 创建当前工作空间下
# os.mkdir('abc/cc/dd') #不能递归创建
# os.makedirs('abd/dd/cc') # 创建当前项目路径下 可以递归创建
# flg = os.path.exists('./abd/dd/cc')
# flg = os.path.exists('./a/dd/cc') # w:4可读r:2可写1:可操作
# if not flg:
# os.makedirs('./a/dd/cc')
# # if not os.path.exists('./')
# print('是否存在路径',flg)
# os.rmdir('./a/dd/cc') #删除最里面那个目录
# os.remove('') #删除文件夹

# 系统命令
# os.system('python3 test.py')
# os.system('ls')

'''
os.path系统模块中的路径模块
'''
# 相对路径转化为绝对路径
res = os.path.abspath('./')
print(res)


res = os.path.dirname(res) #返回路径部分
print(res)

# 路径拼接
os.path.join(os.getcwd(),'2.jpg')


# os.path.isdir() #是否是文件夹
# os.path.isfile() #是否是文件
# os.path.exists()#检测路径和文件

'''
高级写法复制
'''
import shutil
# shutil.copytree() #复制


'''
压缩文件
'''
import zipfile

# with zipfile.ZipFile('cyz1.zip','w') as w:
# w.write('./data.txt')

'''
解压文件
# '''
with zipfile.ZipFile('cyz1.zip','r') as w:
w.extractall('./')


# 使用shutill进行归档压缩
# 参数1:创建压缩文件名称 参数2 :压缩格式 参数三:要压缩的文件文件夹
shutil.make_archive('zx','tar','./')
posted @ 2022-02-28 14:08  冬天里的狐狸  阅读(32)  评论(0编辑  收藏  举报