概述

os.stat() 方法用于在给定的路径上执行一个系统 stat 的调用。
语法

stat()方法语法格式如下:

os.stat(path)

参数

path -- 指定路径

返回值

stat 结构:

st_mode: inode 保护模式
    st_ino: inode 节点号。
    st_dev: inode 驻留的设备。
    st_nlink: inode 的链接数。
    st_uid: 所有者的用户ID。
    st_gid: 所有者的组ID。
    st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
    st_atime: 上次访问的时间。
    st_mtime: 最后一次修改的时间。
    st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。

实例

以下实例演示了 stat() 方法的使用:

#!/usr/bin/python3

import os, sys

# 显示文件 "a2.py" 信息
statinfo = os.stat('a2.py')

print (statinfo)

执行以上程序输出结果为:

posix.stat_result(st_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st
_nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498089, st_mtime=13
30498089, st_ctime=1330498089)

正如你上面看到的,你可以直接访问到这些属性值。

好了,下面我来看看python中的stat模块,先看看自带的例子:

import os, sys
from stat import * def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file''' for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname).st_mode
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname def visitfile(file):
print 'visiting', file if __name__ == '__main__':
walktree(sys.argv[1], visitfile)

可以这么理解,os.stat是将文件的相关属性读出来,然后用stat模块来处理,处理方式有多重,就要看看stat提供了什么了。

1.  可以对st_mode做相关的判断,如是否是目录,是否是文件,是否是管道等。

先看一下处理os.stat返回的st_mode结果的函数,就想上面的例子中的一样,这些函数可以做出判断:

if stat.S_ISREG(mode):           #判断是否一般文件
print 'Regular file.'
elif stat.S_ISLNK (mode): #判断是否链接文件
print 'Shortcut.'
elif stat.S_ISSOCK (mode): #判断是否套接字文件
print 'Socket.'
elif stat.S_ISFIFO (mode): #判断是否命名管道
print 'Named pipe.'
elif stat.S_ISBLK (mode): #判断是否块设备
print 'Block special device.'
elif stat.S_ISCHR (mode): #判断是否字符设置
  print 'Character special device.'
elif stat.S_ISDIR (mode):         #判断是否目录
  print 'directory.'
##额外的两个函数
stat.S_IMODE (mode): #返回文件权限的chmod格式
  print 'chmod format.'
stat.S_IFMT (mode): #返回文件的类型
  print 'type of fiel.'
 

2.   还有一些是各种各样的标示符,这些标示符也可以在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.          对于其他组执行的权限

例子:我想获得某个文件的属性信息,并查看他的权限信息,用chmod的格式显示出来。
复制代码

>>> import stat
>>> import os
>>> st = os.stat('sig.txt')
>>> mode = st.st_mode
>>> stat.S_IFMT(mode)
32768
>>> stat.S_IMODE(mode)
438
>>> print oct(stat.S_IMODE(mode))#oct 是转换为八进制
0666

Python3 os.stat() 方法的更多相关文章

  1. python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别

    python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别 os.path.realpath 获取当前执行脚本的绝对路径. os.path.rea ...

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

    Python第二十二天   stat模块  os.chmod方法  os.stat方法  pwd  grp模块 stat模块描述了os.stat(filename)返回的文件属性列表中各值的意义,根据 ...

  3. python多进程--------linux系统中python的os.fork()方法

    linux下python 创建子进程的原理: os.fork()方法 的原理 为了实现并发.多任务,我们可以在主程序种开启一个进程或者线程.在类unix操作系统当中(非windows),可以用pyth ...

  4. 【转】Python3 操作符重载方法

    Python3 操作符重载方法 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/70769628 以下代码 ...

  5. Python os.chdir() 方法

    概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...

  6. Python os.access() 方法

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

  7. Python os.walk() 方法遍历文件目录

    概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...

  8. Python os.popen() 方法

    简述 就是新建一个管道执行一个命令. 方法是os.popen(命令,权限,缓冲大小) 比如 a = 'mkdir def' b = os.popen(a,) print b 就是等同于使用命令去创建了 ...

  9. Python入门之os.walk()方法

    os.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元 ...

随机推荐

  1. 简单shell实现局域网IP扫描

    #!/bin/bash network=$1 time=$(date +%H%M%S) for i in $(seq $2 $3) do ping -c 1 -w 2 $network.$i > ...

  2. opencv-python教程学习系列8-opencv图像算术运算

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍图像的算术运算,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统环境 ...

  3. Navicat #1045 - Access denied for user 'root'@'localhost' (using password: NO)

    Navicat #1045 - Access denied for user 'root'@'localhost' (using password: YES) 出现上述问题,原因在于本机还开了APMS ...

  4. HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)

    Sample Input 3 9 8 7 5 1 9 2 6 4 3 1 2 3 4 5 6 7 8 9 9 8 7 5 1 9 2 6 4 3 1 2 5 4 3 6 7 8 9 9 8 7 5 1 ...

  5. idea_快捷键default&eclipse

    关键字: SpringMybatisplusRedisApplicationTests github关键字(springboot拦截器完整项目): implements WebMvcConfigure ...

  6. device public set

    backgroud:  our dvertiser provide on device list of idfa to show ad to  target audience,however none ...

  7. svn分支开发注意事项

    1.切换的时候最好查看本文件的是主干上的还是分支上的, 单击右键,点击属性,可以看到以下图片,其中"URL"就可以 看到是主干还是分支 2.切换到分支 点击切换后就选择要切换到的路 ...

  8. 关于 Vue 方法前面的美元符号

    关于 Vue 方法前面的美元符号 学到这一段,不明白什么意思,然后手贱把 $ 删除了,出现未定义方法. vm.$watch('counter', function(nval, oval) { aler ...

  9. 【转】每天一个linux命令(58):telnet命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/13/2956992.html telnet命令通常用来远程登录.telnet程序是基于TELNET ...

  10. oracle之 手动创建 emp 表 与 dept 表

    说明: 有时候我们需要通用的实验数据,emp表 与 dept表  但是数据库中有没有. 这时,我们可以手动创建. -- 创建表与数据CREATE TABLE EMP(EMPNO NUMBER(4) N ...