Discuss / Python / 作业一(需要查看os.path和time模块文档),从而实现命令行下类似dir的效果

作业一(需要查看os.path和time模块文档),从而实现命令行下类似dir的效果

Topic source
import os,time

currentdir = os.getcwd()
filenum, filesize, dirnum = 0, 0, 0
for name in os.listdir(currentdir):
    if os.path.isfile(name):
        print('%s\t\t%d\t%s' % (time.strftime('%Y/%m/%d %H:%M', time.localtime(os.path.getmtime(name))), os.path.getsize(name), name))
        filenum += 1
        filesize += os.path.getsize(name)
    elif os.path.isdir(name):
        print('%s\t<DIR>\t\t%s' % (time.strftime('%Y/%m/%d %H:%M', time.localtime(os.path.getmtime(name))), name))
        dirnum += 1

print('\t\t%d个文件\t\t%d 字节' % (filenum, filesize))
print('\t\t%d个目录' % dirnum)

能不能解释下题目的意思啊

个人理解题目是想要模拟实现命令行下输入dir之后得到的效果。


  • 1

Reply