Discuss / Python / 交作业 第二题

交作业 第二题

Topic source

import os, stat, time  #导入模块

def dir_list(path,key=''):

    for name in os.listdir(path):   #遍历

        if( name != 'Internet 临时文件'):#有个文件夹不能访问

            fn = path + '/' + name      #名字加路径

            if(os.path.isdir(fn)):      #目录 继续递归

                dir_list(fn,key)

            elif key in name: #过滤打印               

                mt = os.path.getmtime(fn)   #逐个项目

                t = time.strftime('%Y/%m/%d  %H:%M',time.localtime(os.stat(fn).st_mtime))

                ty = ' <DIR> ' if os.path.isdir(fn) else '       '

                sz = os.path.getsize(fn)

                print("%s%s%6u%s" % (t, ty, sz, name))  #控制打印

dir_list('.','123')   #从当前目录遍历    测试‘123’ ‘abc’  'test'


  • 1

Reply