# python学习笔记(三)装饰器以及os模块(对文件大小的统计以及筛选)

回顾

手动抛出异常

raise 异常名称(“输出的提示信息”)

例如:

raise NameError("我出错了")
Traceback (most recent call last):
  File "C:/Users/xlg/PycharmProjects/shPython1701/day8文件操作/1raise.py", line 2, in <module>
    raise NameError("我出错了")
NameError: 我出错了

一 装饰器

def demo(x):
    def inner(age):
        if age<=10:
            myStr = "儿童"
        elif age<=20:
            myStr = "青年"
        else:
            myStr = "老年人"
        x(myStr)
    return inner
# func = demo(func)
# func(10)
@demo #func = demo(func)
def func(people):
  print("你现在已经步入了{}阶段了".format(people))
func(30)

二 os模块

如果想使用os模块 那么你需要先导入 os模块

import os

        函数名                              函数的说明                  
      os.name           获取操作系统的类型(nt->windows  posix->Linux,Unix)
    os.environ                        获取操作系统中的环境变量              

os.environ.get(“path”) 获取某一个的环境变量
os.curdir 获取当前的目录
os.getcwd() 获取当前工作目录的绝对路径
os.listdir() 以列表的形式 返回 当前目录下的所有文件
os.mkdir() 创建目录
os.rmdir() 删除文件夹
os.rename(old,new) 重命名
os.path.join(path,file) 拼成一个正确的路径
os.path.splitext() 获取文件扩展名
os.path.isdir() 判断是否是目录
os.path.isfile() 判断是否是文件
os.path.exists() 判断文件或者路径是否存在
os.path.getsize() 获取文件的大小(字节)
os.path.dirname() 获取目录名
os.path.basename() 获取文件名
os.path.abspath() 获取一个文件的绝对路径
os.path.split() 拆分路径

利用递归统计文件的大小:

import os
path = r'C:\Users\acer\Desktop\许金涛 作业'
def totalSize(path):
    sum = 0
    fileListr = os.listdir(path)  # 返回目录下的所有文件 以列表的形式
    for file in fileListr:  # 进行遍历 获取每一个文件
        # print(file)
        newPath = os.path.join(path, file) # 将每一个文件拼接成一个绝对路径
        if os.path.isdir(newPath):  # 判断是否为目录
            sum += totalSize(newPath)  # 累加每个目录的大小
        elif os.path.isfile(newPath):  # 判断是否为文件
            sum += os.path.getsize(newPath)  # 累加每个文件的大小
    return sum
print(totalSize(path))

通过递归检索目录下为py的所有文件:

import os
myList = []
def getHouZhui(path, myList):  # 获取文件后缀的函数
    myFileList = os.listdir(path)  # 返回当前目录下的所有文件 以列表形式
    for file in myFileList:
        # print(file)
        newPath = os.path.join(path, file)  # 拼接成一个完整的路径
        if os.path.isdir(newPath):  # 判断是否为目录
            getHouZhui(newPath, myList)  # 调用自己
        elif newPath[-2: ].upper() == 'PY':  # 判断文件后缀是否为PY
            myList.append(file)  # 将py后缀的文件添加到列表里
def main():
    while True:
        path = input('请输入你要查询后缀的路径:')
        if os.path.exists(path):  # 判断是否是一个存在的正确的路径
            break   # 如果存在则跳出循环
        else:  # 如果不存在, 则重新输入
            print('您输入的路径不存在 请重新输入')
    getHouZhui(path, myList)  # 调用获取后缀的函数
    print(myList)   # 将带有后缀的函数打印出来
main()

# 用堆栈的形式获取目录下的所有的文件名
path = 'D:\python35'
myList = []
fileList = os.listdir(path)
for i in fileList:
    if os.path.isfile(i):
        myList.append(i)
    else:
        newPath = os.path.join(path, i)

用堆栈的形式获取目录下所有的文件名:

import os
path = 'D:\python35'
myList = []
myList.append(path)
while len(myList) != 0:
    myPath = myList.pop()
    myListDir = os.listdir(myPath)  # 返回当前目录下的所有文件
    for fileName in myListDir:
        # print(fileName)
        newPath = os.path.join(path, fileName)  # 拼凑一个新的完整路径
        if os.path.isdir(newPath):
            myList.append(newPath)
        else:
            print('文件名', fileName)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值