Discuss / Python / 关于第一题的dir -l问题

关于第一题的dir -l问题

Topic source

久疤_796

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

老师的意思应该是显示当前目录下所有内容的详细信息

这个貌似是Linux下的命令 http://www.poluoluo.com/server/200911/71341.html

久疤_796

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

借助老师的代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'HZF'

'''
实现dir -l功能
'''
from datetime import datetime
import sys
import os

def dirL(cmd,pwd):
    pwd = os.path.abspath(pwd)
    permission = 0
    if sys.argv[1] in ['-l','-L']:
        for f in os.listdir(pwd):
            try:
                p = os.path.join(pwd,f)
                fsize = os.path.getsize(p)
                mtime = datetime.fromtimestamp(os.path.getmtime(p)).strftime('%Y-%m-%d %H:%M')
                flag = '/' if os.path.isdir(p) else ''
                print('%10d  %s  %s%s' % (fsize, mtime, f, flag))
            except PermissionError as e:
                permission+=1
        if permission:
            print('拒绝访问数:%d'%permission)
    else:
        print('参数内容错误:%s'%sys.argv[1])

if __name__=='__main__':
    #print(len(sys.argv),sys.argv)
    if len(sys.argv) == 2:
        dirL(sys.argv[1],'.')
    elif len(sys.argv) == 3:
        dirL(sys.argv[1],sys.argv[2])
    else:
        print('参数个数错误')

现象:

K:\mySetupDir\work\Python3-4\workSpace\mycompany2\IO编程>mydir -l
         0  2015-12-20 20:21  1.txtc
      1080  2015-12-20 23:08  mydir.py
       811  2015-12-19 17:31  StringIO和BytesIO.py
       100  2015-12-19 16:33  test.txt
         0  2015-12-20 21:54  testdir/
      3223  2015-12-20 22:48  操作文件和目录.py
      1648  2015-12-19 16:44  文件读写.py
      2305  2015-12-19 15:48  这里的IO方式为同步IO,因为异步IO较为复杂,后面再讲
.txt

久疤_796

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

补充:mydir -l这是在CMD下调用的


  • 1

Reply