Discuss / Python / Daily Work: program 1

Daily Work: program 1

Topic source

无愠无殇

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
__author__ = 'admin'
import os
import time

'''
     日期  时间  目录<dir>        目录没size    文件名
                  文件 默认为空格  文件size
                 文件   XXXXX字节
                 目录        XXXXX字节
     st_mtime (修改时间)
'''
# #查看文件的大小
# print(statinfo.st_size)
# #查看是否为目录
# print(os.path.isdir(x))
# #查看当前路径
# print(os.path.abspath('.'))
def dirL():
    currentpath = os.getcwd()
    direction = os.listdir(currentpath)
    dirl = [x.lower() for x in direction]
    print(dirl)
    direSize, fileSize, dirnum, filenum = 0, 0, 0, 0
    for x in dirl:
        if os.path.isdir(x):
            dirnum = dirnum + 1
            direSize = direSize + os.path.getsize(x)
        else:
            filenum = filenum + 1
            fileSize = fileSize + os.path.getsize(x)
    def prepo(x):
        #查看文件的属性,st_ctime:最近修改时间,st_size文件大小
        statinfo = os.stat(x)
        timeArray = time.localtime(statinfo.st_ctime)
        regTime = time.strftime("%Y/%m/%d  %H:%M", timeArray)
        size = statinfo.st_size
        if os.path.isdir(x):
            return regTime + '  ' + str(size) +' ' + x
        else:
            return regTime + '   <DIR>' +'  '+ str(size) + '  ' + x
    # def getSize():
    #     totalSize =10* ' '+ str(dirnum)+'个目录' + str(direSize) +'字节' +'\n'
    #     10* ' '+ str(filenum)+'个文件' + str(fileSize) +'字节'
    L = map(prepo,dirl)
    return list(L)
[print(x) for x in dirL()]

无愠无殇

#2 Created at ... [Delete] [Delete and Lock User]

dir /l 缺少功能:统计目录和文件的总字节

# -*- coding: utf-8 -*-
__author__ = 'admin'
import os
import time

# #查看文件的大小
# print(statinfo.st_size)
# #查看是否为目录
# print(os.path.isdir(x))
# #查看当前路径
# print(os.path.abspath('.'))
def dirL():
    currentpath = os.getcwd()
    direction = os.listdir(currentpath)
    dirl = [x.lower() for x in direction]
        #查看文件的属性,st_ctime:最近修改时间,st_size文件大小
        statinfo = os.stat(x)
       #格式化时间戳
        timeArray = time.localtime(statinfo.st_ctime)
        regTime = time.strftime("%Y/%m/%d  %H:%M", timeArray)
        size = statinfo.st_size
        if os.path.isdir(x):
            return regTime + '  ' + str(size) +' ' + x
        else:
            return regTime + '   <DIR>' +'  '+ str(size) + '  ' + x
    L = map(prepo,dirl)
    return list(L)
[print(x) for x in dirL()]

  • 1

Reply