Discuss / Python / 第一题较笨的解决方法

第一题较笨的解决方法

Topic source

Tommy_Chiu

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

关于第一题使用os模块输出类似dir -l的格式,dir -l是Linux系统的命令,格式如下 [root@test bin]# dir -l /tmp total 18572 -rw-r--r-- 1 root root 29 May 31 21:56 ip_addr -rw------- 1 root root 7 May 26 21:40 passwd -rw-r--r-- 1 root root 1197370 Jun 3 21:29 pip-9.0.1.tar.gz drwx------ 2 root root 4096 Jun 3 23:36 pymp-HrY79Q drwxr-xr-x 18 1000 1000 4096 Jun 3 21:32 Python-2.7.13 -rw-r--r-- 1 root root 17076672 Jun 3 21:27 Python-2.7.13.tgz drwxr-xr-x 9 root root 4096 Jun 3 21:42 setuptools-36.0.1 -rw-r--r-- 1 root root 711296 Jun 3 21:42 setuptools-36.0.1.zip drwx------ 3 root root 4096 May 26 22:29 tmpwm3eHe -rw-------. 1 root root 0 May 26 20:23 yum.log

对于没接触过Linux的童鞋,关于以上输出的各列的意思,可以参考“鸟哥的Linux私房菜”以下章节: http://cn.linux.vbird.org/linux_basic/0210filepermission_2.php

个人水平可能不高,写的有点多,其中一些代码看有没有代替的模块或者高手的代码能参考,凑合着看吧。。。有其他高手能写出更简洁的最好了

总共用了4个模块,其中核心是os模块,time和pwd,grp模块只是用于对时间以及用户、所属组的操作

!/usr/bin/python

coding=utf-8

import os import time import pwd,grp from os import path,stat

def list_dir(p):

'''
将代表文件权限的数字转换成dir -l输出格式
'''
def perm(p):    
    return {'0':'---','1':'--x','2':'-w-','3':'-wx','4':'r--','5':'r-x','6':'rw-','7':'rwx'}[p]

'''
将代表文件类型的数字转换成dir -l输出格式
'''
def file_type(p):
    return {'040':'d','041':'d','0100':'-','020':'c','060':'b','010':'p','0140':'s'}[p]

'''
使用pwd和grp模块,根据os.stat(path).st_uid和st_gid,将uid和gid号转化成用户和所属的组,如果操作系统没有对应的用户和所属的组,返回uid和gid本身
'''
def get_file_owner(p):
    stat_info=stat(p)
    uid=stat_info.st_uid
    gid=stat_info.st_gid
    try:
        user=pwd.getpwuid(uid)[0]
        group=grp.getgrgid(gid)[0]
        return user,group
    except:
        return str(uid),str(gid)
'''
使用time模块转换时间,dir -l的时间默认是文件、目录等的修改时间,通过os.path.getmtime获取修改时间,然后和本地时间进行对比。os.path.getmtime和time.time()返回的是时间戳,两者可以进行加减运算,详情可以查官方文档关于time模块的说明

Linux对于时间久远的文件或者目录等的输出是用年月日,这里定义如果修改时间在一年前的,显示年月日,否则显示年月时分
'''
def modify_time(p):
    mtime=path.getmtime(p)
    ltime=time.time()
            if abs(ltime-mtime)>float(31556926):
        return time.strftime('%b %d %Y',time.localtime(path.getmtime(p)))
    else:
        return time.strftime('%b %d %H:%M',time.localtime(path.getmtime(p)))

'''
主体地方,主要利用os.path,os.stat返回的属性结合之前的函数,输出对应的列
'''
for item in os.listdir(p):
    fullpath=path.join(p,item)
    f_type=file_type(oct(stat(fullpath).st_mode)[:-3])
    file_perm=''.join(map(perm,(oct(stat(fullpath).st_mode))[-3:]))
    link=str(stat(fullpath).st_nlink)
    user,group=get_file_owner(fullpath)
    size=str(path.getsize(fullpath))
    mtime=modify_time(fullpath)
    print f_type+file_perm+link.rjust(4)+' '+user+' '+group+' '+size.rjust(10)+' '+mtime.rjust(2)+' '+item

list_dir('/var')

输出如下

[root@test ~]# python exam.py drwxr-xr-x 2 root root 4096 May 26 20:30 crash drwxr-xr-x 9 root root 4096 Jun 17 20:59 log drwxr-xr-x 9 root root 4096 Jun 17 21:43 cache drwxr-xr-x 2 root root 4096 Sep 23 2011 games drwxrwxrwx 2 root root 4096 Jun 17 21:44 tmp drwxr-xr-x 3 root root 4096 May 26 20:27 db drwxr-xr-x 2 root root 4096 Sep 23 2011 yp drwxr-xr-x 31 root root 4096 May 26 22:26 lib drwxr-xr-x 2 root root 4096 Sep 23 2011 local drwxr-xr-x 3 root root 4096 May 26 20:27 empty drwxr-xr-x 2 root root 4096 Nov 22 2013 cvs drwxr-xr-x 8 root root 4096 May 26 23:12 www drwxr-xr-x 2 root root 4096 Sep 23 2011 nis drwxr-xr-x 2 root root 4096 Sep 23 2011 opt drwxr-xr-x 22 root root 4096 Jun 17 21:44 run drwxrwxr-x 2 root mail 4096 Jun 18 00:44 mail drwxrwxr-x 6 root lock 4096 Jun 17 20:59 lock drwxr-xr-x 2 root root 4096 Sep 23 2011 preserve drwxr-xr-x 8 root root 4096 May 26 20:25 spool [root@test ~]#


  • 1

Reply