zoukankan      html  css  js  c++  java
  • Python第二十二天 stat模块 os.chmod方法 os.stat方法 pwd grp模块 os.access()方法

    Python第二十二天   stat模块  os.chmod方法  os.stat方法  pwd  grp模块  os.access()方法

    stat模块描述了os.stat(filename)返回的文件属性列表中各值的意义,根据stat模块读取os.stat()中的值的意思
    简单来说,os.stat是将文件的相关属性读出来,然后用stat模块来处理

    os.stat

    返回一个类似字典对象(stat_result对象,包含10个元素),结果类型是posix.stat_result

    stat(path) -> stat result 

    获取stat结果

    st = os.stat('/tmp/aa.py')
    print st
    posix.stat_result(st_mode=33188, st_ino=385537, st_dev=64513L, st_nlink=1, st_uid=0, st_gid=0, st_size=1243, st_atime=1505466749, st_mtime=1505870370, st_ctime=1505870370)
    
    st_mode(权限模式)
    st_ino(inode number)
    st_dev(device)
    st_nlink(number of hard links)
    st_uid(所有用户的user id)
    st_gid(所有用户的group id)
    st_size(文件大小,以位为单位)
    st_atime(最近访问的时间)
    st_mtime(最近修改的时间)
    st_ctime(创建的时间)
    
    
    os.stat的返回类型
    type(st)
    posix.stat_result
    
    
    st = os.stat('/tmp/aa.py')
    st.st_ctime
    stat.ST_CTIME(st.st_ctime)
    
    st.st_ino
    stat.ST_INO(st.st_ino)
    
    
    st.st_dev
    stat.ST_DEV(st.st_dev)
    
    st.st_nlink
    stat.ST_NLINK(st.st_nlink)
    
    st.st_uid
    stat.ST_UID(st.st_uid)
    
    st.st_gid
    stat.ST_GID(st.st_gid)
    
    
    st.st_size
    stat.ST_SIZE(st.st_size)
    
    
    st.st_atime
    stat.ST_ATIME(st.st_atime)
    
    
    st.st_mtime
    stat.ST_MTIME(st.st_mtime)
    
    
    st.st_ctime
    stat.ST_CTIME(st.st_ctime)
    
    
    st.st_mode
    stat.S_IMODE(st.st_mode)

    stat模块

    import stat

    stat 模块定义用来测试文件类型的函数
    stat.S_ISDIR(mode)
      判断文件是不是一个目录。

    stat.S_ISCHR(mode)
      判断文件是不是一个字符型设备。

    stat.S_ISBLK(mode)
      判断文件是不是一个块设备。

    stat.S_ISREG(mode)
      判断mode是不是来自一个普通文件。

    stat.S_ISFIFO(mode)
      判断mode是不是来自一个FIFO(如:具名管道)

    stat.S_ISLNK(mode)
      判断mode是不是来自一个符号链接。

    stat.S_ISSOCK(mode)
      判断mode是不是来自一个套接字。

    stat.S_IMODE(mode)
      #返回文件权限的chmod格式。

    stat.S_IFMT(mode)  
       返回文件的类型



    例子
    st = os.stat('/tmp/aa.py').st_mode
    oct(stat.S_IMODE(st))
    '0755'



    stat 模块的标识符,可以用在os.chmod()方法中:

    stat.S_ISUID: Set user ID on execution.                      不常用
    stat.S_ISGID: Set group ID on execution.                    不常用
    stat.S_ENFMT: Record locking enforced.                                          不常用
    stat.S_ISVTX: Save text image after execution.                                在执行之后保存文字和图片
    stat.S_IREAD: Read by owner.                                                           对于拥有者读的权限
    stat.S_IWRITE: Write by owner.                                                         对于拥有者写的权限
    stat.S_IEXEC: Execute by owner.                                                       对于拥有者执行的权限
    stat.S_IRWXU: Read, write, and execute by owner.                          对于拥有者读写执行的权限
    stat.S_IRUSR: Read by owner.                                                            对于拥有者读的权限
    stat.S_IWUSR: Write by owner.                                                          对于拥有者写的权限
    stat.S_IXUSR: Execute by owner.                                                       对于拥有者执行的权限
    stat.S_IRWXG: Read, write, and execute by group.                                 对于同组的人读写执行的权限
    stat.S_IRGRP: Read by group.                                                             对于同组读的权限
    stat.S_IWGRP: Write by group.                                                           对于同组写的权限
    stat.S_IXGRP: Execute by group.                                                        对于同组执行的权限
    stat.S_IRWXO: Read, write, and execute by others.                          对于其他组读写执行的权限
    stat.S_IROTH: Read by others.                                                           对于其他组读的权限
    stat.S_IWOTH: Write by others.                                                         对于其他组写的权限
    stat.S_IXOTH: Execute by others.                                                      对于其他组执行的权限


    os.access() 方法


    这个方法一般只用来做判断

    os.access() 方法使用当前的uid/gid尝试访问路径。大部分操作使用有效的 uid/gid, 因此运行环境可以在 suid/sgid 环境尝试

    os.access(path, mode);
    参数
    path -- 要用来检测是否有访问权限的路径。
    mode -- mode为F_OK,测试存在的路径,或者它可以是包含R_OK, W_OK和X_OK或者R_OK, W_OK和X_OK其中之一或者更多。
    os.F_OK: 作为access()的mode参数,测试path是否存在。
    os.R_OK: 包含在access()的mode参数中 , 测试path是否可读。
    os.W_OK 包含在access()的mode参数中 , 测试path是否可写。
    os.X_OK 包含在access()的mode参数中 ,测试path是否可执行

    例子1

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    import os, sys
     
    # 假定 test/foo.txt 文件存在,并有读写权限
    
    ret = os.access("test/foo.txt", os.F_OK)
    print "F_OK - 返回值 %s"% ret
     
    ret = os.access("test/foo.txt", os.R_OK)
    print "R_OK - 返回值 %s"% ret
     
    ret = os.access("test/foo.txt", os.W_OK)
    print "W_OK - 返回值 %s"% ret
     
    ret = os.access("test/foo.txt", os.X_OK)
    print "X_OK - 返回值 %s"% ret

    例子2

    def check_access_and_print_warning(sock_dir):
        '''
        Check if this user is able to access the socket
        directory and print a warning if not
        '''
        if (os.access(sock_dir, os.R_OK) and
                os.access(sock_dir, os.W_OK) and
                os.access(sock_dir, os.X_OK)):
            return
        else:
            print('WARNING: Events will not be reported'
                  ' (not able to access {0})'.format(sock_dir))

    os.chmod() 方法

    os.chmod() 方法用于更改文件或目录的权限。
    语法
    chmod()方法语法格式如下,该方法没有返回值:
    os.chmod(path, mode)


    参数
    path -- 文件名路径或目录路径。
    flags -- 可用以下选项按位或进行权限叠加。


    stat.S_IXOTH: 其他用户有执行权0o001
    stat.S_IWOTH: 其他用户有写权限0o002
    stat.S_IROTH: 其他用户有读权限0o004
    stat.S_IRWXO: 其他用户有全部权限(权限掩码)0o007
    stat.S_IXGRP: 组用户有执行权限0o010
    stat.S_IWGRP: 组用户有写权限0o020
    stat.S_IRGRP: 组用户有读权限0o040
    stat.S_IRWXG: 组用户有全部权限(权限掩码)0o070
    stat.S_IXUSR: 拥有者具有执行权限0o100
    stat.S_IWUSR: 拥有者具有写权限0o200
    stat.S_IRUSR: 拥有者具有读权限0o400
    stat.S_IRWXU: 拥有者有全部权限(权限掩码)0o700
    stat.S_ISVTX: 目录里文件目录只有拥有者才可删除更改0o1000
    stat.S_ISGID: 执行此文件其进程有效组为文件所在组0o2000
    stat.S_ISUID: 执行此文件其进程有效用户为文件所有者0o4000
    stat.S_IREAD: windows下设为只读
    stat.S_IWRITE: windows下取消只读

    使用os.chmod()执行chmod +x

    # 获取到文件的权限模式,然后将原来权限模式和新权限模式用或连接起来就可以实现chmod+x
    stmode = os.stat('/tmp/aa.py').st_mode
    os.chmod('/tmp/aa.py', stmode | stat.S_IXOTH|stat.S_IXGRP |stat.S_IXUSR)

    pwd模块和grp模块

    pwd模块,提供了一个Unix 密码数据库(/etc/passwd文件)的接口,这个数据库包含本地机器用户账户信息。

    返回对应uid的用户信息 

    pwd.getpwuid(uid)

    返回对应name的用户信息 

    pwd.getpwnam(username)

    获取返回结果中的uid

    pwd.getpwnam(username)
    pwd.struct_passwd(pw_name='mysql', pw_passwd='x', pw_uid=500, pw_gid=500, pw_gecos='', pw_dir='/home/mysql', pw_shell='/sbin/nologin')
    
    
    def setPrint():
    print pwd.getpwnam('mysql')
    list=[]
    for i in pwd.getpwnam('mysql'):
    print i
    
    # 通过列表保存返回的结果
    list.append(i)
    # print type(aa)
    
    print list
    uid = list[2]  # 列表第三个元素就是uid
    print 'uid %s' %(uid)
    
     

    返回所有用户信息

    pwd.getpwall()

     返回所有用户信息

    import pwd
    def get_user():
      all_user = {}
      for user in pwd.getpwall():
        all_user[user[0]] = all_user[user[2]] = user
      return all_user
    def userinfo(uid):
      return get_user()[uid]

    grp模块,提供了一个Unix 用户组/group(/etc/group文件)数据库的接口

    返回对应gid的组信息 

    grp.getgrgid(gid)

    返回对应group name的组信息 

    grp.getgrname(groupname)

    返回所有组信息 

    grp.getgrall()
  • 相关阅读:
    Java AbstractQueuedSynchronizer(AQS)
    CentOS Install Rancher & K3s
    CentOS Install Rancher & Kubernetes
    CentOS Install Kubernetes & Kubesphere
    支付宝小程序入门
    uni-app 抽奖
    APP上架
    uniapp微信小程序语音识别实现
    uniapp 安卓打包之导入的项目打包指导
    uniapp 支付宝 支付后不成功回调
  • 原文地址:https://www.cnblogs.com/lyhabc/p/7572062.html
Copyright © 2011-2022 走看看