糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > Python第二十二天 stat模块 os.chmod方法 os.stat方法 pwd grp模块

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

时间:2021-08-11 08:41:27

相关推荐

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

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

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 stposix.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_resultst = os.stat('/tmp/aa.py')st.st_ctimestat.ST_CTIME(st.st_ctime)st.st_inostat.ST_INO(st.st_ino)st.st_devstat.ST_DEV(st.st_dev)st.st_nlinkstat.ST_NLINK(st.st_nlink)st.st_uidstat.ST_UID(st.st_uid)st.st_gidstat.ST_GID(st.st_gid)st.st_sizestat.ST_SIZE(st.st_size)st.st_atimestat.ST_ATIME(st.st_atime)st.st_mtimestat.ST_MTIME(st.st_mtime)st.st_ctimestat.ST_CTIME(st.st_ctime)st.st_modestat.S_IMODE(st.st_mode)

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.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+xstmode = os.stat('/tmp/aa.py').st_modeos.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 listuid = list[2] # 列表第三个元素就是uidprint 'uid %s' %(uid)

返回所有用户信息

pwd.getpwall()

返回所有用户信息

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

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

返回对应gid的组信息

grp.getgrgid(gid)

返回对应group name的组信息

grp.getgrname(groupname)

返回所有组信息

grp.getgrall()

如果觉得《Python第二十二天 stat模块 os.chmod方法 os.stat方法 pwd grp模块》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。