Discuss / Python / dir -l 目录读取部分仿照老师的代码写的,其他的查资料自己写的

dir -l 目录读取部分仿照老师的代码写的,其他的查资料自己写的

Topic source

枇杷大人

#1 Created at ... [Delete] [Delete and Lock User]
#-*- coding:utf-8 -*-

__author__ = '打补丁的狮子'

import sys
import os
from datetime import datetime

def myDirList():
    currpath = os.path.abspath('.')
    print("————————————%s————————————" % currpath)
    for f in os.listdir(currpath):
        fsize = os.path.getsize(f)
        mtime = datetime.fromtimestamp(os.path.getmtime(f)).strftime('%Y-%m-%d %H:%M')
        if os.path.isdir(f):
            #flag = '<dir>' if os.path.isdir(f) else ''
            #print('%10d %s   %5s   %s' % (fsize, mtime, flag, f))
            print('%10d %s   <dir>   %s' % (fsize, mtime, f))

if(__name__ == "__main__"):
    if 0==len(sys.argv):
        print("")
    elif 1 <=len(sys.argv):
        if '-l' == sys.argv[1].lower():
            myDirList()
        elif '-?' == sys.argv[1].lower() or '-h' == sys.argv[1].lower():
            print("            -l            显示目录列表")
            print("            -?/-h         显示帮助信息")
        else:
            print("无效的参数,请输入-?或-h参数来获取帮助信息!")

枇杷大人

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

打印结果 ————————————E:\PythonLearn———————————— 4096 2015-12-29 10:13 <dir> .git 0 2015-12-29 10:06 <dir> ostestdir 0 2015-12-28 09:28 <dir> pycache


  • 1

Reply