python 遍历文件获取文件时间

遍历文件方式一

import os
file_path = "E:\\Movie"
for abs_path,dir_path,files in os.walk(file_path):
    for fi in files:
        filename = os.path.join(abs_path, fi)
        print(filename)

遍历文件方式二

import os
file_path = "E:\\Movie"
def get_file(file_dir):
    dir_list = os.listdir(file_dir)
    for di in dir_list:
        file_or_dir = os.path.join(file_dir,di)
        if os.path.isdir(file_or_dir):
            get_file(file_or_dir)
        else:
            print(file_or_dir)

get_file(file_path)

遍历文件获取文件时间

def TimeStampToTime(timestamp):
    timeStruct = time.localtime(timestamp)
    return time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)
    
file_path = "E:\\Movie"

def get_file(file_dir):
    dir_list = os.listdir(file_dir)
    for di in dir_list:
        file_or_dir = os.path.join(file_dir,di)
        if os.path.isdir(file_or_dir):
            get_file(file_or_dir)
        else:
            print(file_or_dir)
            t = os.path.getctime(filename)
            t_str = TimeStampToTime(t)
            print(t_str)

get_file(file_path)

你可能感兴趣的:(python 遍历文件获取文件时间)