Discuss / Python / 交作业了

交作业了

Topic source

import os

import time

def dir_l(path):

    file_num, file_size, dir_num = 0, 0, 0

    for name in os.listdir(path):

        new_path = os.path.join(path, name)

        if os.path.isfile(new_path):

            print('文件名:%s,文件大小:%sB,最后访问时间:%s' % (name, os.path.getsize(new_path),

                                                              time.strftime('%y-%m-%d %H:%M', time.localtime(

                                                                  os.path.getmtime(new_path)))))

            file_num += 1

            file_size += os.path.getsize(new_path)

        if os.path.isdir(new_path):

            print('文件夹名:%s,最后访问时间:%s,' % (name, time.strftime('%y-%m-%d %H:%M',

                                                                        time.localtime(os.path.getmtime(new_path)))))

            dir_num += 1

    print('文件夹共%s个;文件共%s个,总内存为%s个字节' % (dir_num, file_num, file_size))

def find_file(path, s):

    for file_name in os.listdir(path):

        new_path = os.path.join(path, file_name)

        if os.path.isfile(new_path) and s in new_path:

            print('address: ', new_path)

            print('文件名: %s, 文件大小: %s B ,最后访问时间: %s' % (file_name, os.path.getsize(new_path),

                                                                    time.strftime('%y-%m-%d %H:%M', time.localtime(

                                                                        os.path.getmtime(new_path)))))

        elif os.path.isdir(new_path):

            find_file(new_path, s)

path = 'G:\\pythonProject'

find_file(path, 'test')


  • 1

Reply