python实现windows下dir功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36528804/article/details/82531604

python实现windows下命令窗口的类似dir功能:

'''
输出absPath路径下的文件及文件夹信息
'''

import os
import time

# 时间输出格式化函数
def TimeStampToTime(timestamp):
    timeStruct = time.localtime(timestamp)
    return time.strftime('%Y/%m/%d  %H:%M', timeStruct)

# 绝对路径absPath 
absPath = 'F:\\python'
for fileName in os.listdir(absPath):
    fileAbsPath = os.path.join(absPath, fileName)
    # >>> os.path.join('a','b') >>> 'a\\b'
    dirModifyTime = TimeStampToTime(os.path.getmtime(fileAbsPath))

    isDir = None
    dirSize = None
    if os.path.isdir(fileAbsPath):
        isDir = '<DIR>'
        dirSize = ''
    if os.path.isfile(fileAbsPath):
        isDir = ''
        dirSize = str( format(os.path.getsize(fileAbsPath), ',') )

    print(dirModifyTime, isDir.center(11, ' '), dirSize.rjust(8, ' '), fileName)

效果如下:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_36528804/article/details/82531604