当前位置: 首页 > 编程日记 > 正文

(原创)Python文件与文件系统系列(5)——stat模块

stat模块中定义了许多的常量和函数,可以帮助解释 os.stat()、os.fstat()、os.lstat()等函数返回的 st_result 类型的对象。

通常使用 os.path.is*() 这类函数来测试一个文件的类型,这些方法对同一个文件进行多次测试时,stat()系统调用都是不可避免的开销。同时,有些信息是os.path.is*() 这类函数无法提供的,例如检测是否是块设备、字符设备等。

此时就可以使用 stat 模块及stat模块中的诸多功能,下面是一个例子:

import os, sys
import statdef 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_modeif stat.S_ISDIR(mode):# It's a directory, recurse into itwalktree(pathname, callback)elif stat.S_ISREG(mode):# It's a file, call the callback functioncallback(pathname)else:# Unknown file type, print a messageprint 'Skipping %s' % pathnamedef visitfile(file):print 'visiting', fileif __name__ == '__main__':walktree(sys.argv[1], visitfile)

该例子递归遍历命令行参数所指定的目录中的所有普通文件。

例:上面脚本的执行结果

# python stat_test.py data_structure/
visiting data_structure/a.out
visiting data_structure/biThrTree.h
visiting data_structure/biThrTree.c

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是不是来自一个套接字。

上面的这些函数,除了 S_ISDIR 和 S_ISREG,其他都只在 Unix 环境下才有效。

stat.S_IMODE(mode)
返回 mode 中可以被 os.chmod() 函数设置的部分,在Unix平台上,包括:权限位、sticky bits,set-group-id位、set-uid-bit等。
stat.S_IFMT(mode)  
  返回 mode 中描述文件类型(可以被S_IS*()函数使用)的部分。

All the variables below are simply symbolic indexes into the 10-tuple returned by os.stat()os.fstat() or os.lstat().

stat.ST_MODE

Inode protection mode.

stat.ST_INO

Inode number.

stat.ST_DEV

Device inode resides on.

stat.ST_NLINK

Number of links to the inode.

stat.ST_UID

User id of the owner.

stat.ST_GID

Group id of the owner.

stat.ST_SIZE

Size in bytes of a plain file; amount of data waiting on some special files.

stat.ST_ATIME

Time of last access.

stat.ST_MTIME

Time of last modification.

stat.ST_CTIME

The “ctime” as reported by the operating system. On some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time (see platform documentation for details).

The interpretation of “file size” changes according to the file type. For plain files this is the size of the file in bytes. For FIFOs and sockets under most flavors of Unix (including Linux in particular), the “size” is the number of bytes waiting to be read at the time of the call to os.stat()os.fstat(), or os.lstat(); this can sometimes be useful, especially for polling one of these special files after a non-blocking open. The meaning of the size field for other character and block devices varies more, depending on the implementation of the underlying system call.

The variables below define the flags used in the ST_MODE field.

Use of the functions above is more portable than use of the first set of flags:

stat.S_IFSOCK

Socket.

stat.S_IFLNK

Symbolic link.

stat.S_IFREG

Regular file.

stat.S_IFBLK

Block device.

stat.S_IFDIR

Directory.

stat.S_IFCHR

Character device.

stat.S_IFIFO

FIFO.

The following flags can also be used in the mode argument of os.chmod():

stat.S_ISUID

Set UID bit.

stat.S_ISGID

Set-group-ID bit. This bit has several special uses. For a directory it indicates that BSD semantics is to be used for that directory: files created there inherit their group ID from the directory, not from the effective group ID of the creating process, and directories created there will also get the S_ISGID bit set. For a file that does not have the group execution bit (S_IXGRP) set, the set-group-ID bit indicates mandatory file/record locking (see also S_ENFMT).

stat.S_ISVTX

Sticky bit. When this bit is set on a directory it means that a file in that directory can be renamed or deleted only by the owner of the file, by the owner of the directory, or by a privileged process.

stat.S_IRWXU

Mask for file owner permissions.

stat.S_IRUSR

Owner has read permission.

stat.S_IWUSR

Owner has write permission.

stat.S_IXUSR

Owner has execute permission.

stat.S_IRWXG

Mask for group permissions.

stat.S_IRGRP

Group has read permission.

stat.S_IWGRP

Group has write permission.

stat.S_IXGRP

Group has execute permission.

stat.S_IRWXO

Mask for permissions for others (not in group).

stat.S_IROTH

Others have read permission.

stat.S_IWOTH

Others have write permission.

stat.S_IXOTH

Others have execute permission.

stat.S_ENFMT

System V file locking enforcement. This flag is shared with S_ISGID: file/record locking is enforced on files that do not have the group execution bit (S_IXGRP) set.

stat.S_IREAD

Unix V7 synonym for S_IRUSR.

stat.S_IWRITE

Unix V7 synonym for S_IWUSR.

stat.S_IEXEC

Unix V7 synonym for S_IXUSR.

The following flags can be used in the flags argument of os.chflags():

stat.UF_NODUMP

Do not dump the file.

stat.UF_IMMUTABLE

The file may not be changed.

stat.UF_APPEND

The file may only be appended to.

stat.UF_OPAQUE

The directory is opaque when viewed through a union stack.

stat.UF_NOUNLINK

The file may not be renamed or deleted.

stat.UF_COMPRESSED

The file is stored compressed (Mac OS X 10.6+).

stat.UF_HIDDEN

The file should not be displayed in a GUI (Mac OS X 10.5+).

stat.SF_ARCHIVED

The file may be archived.

stat.SF_IMMUTABLE

The file may not be changed.

stat.SF_APPEND

The file may only be appended to.

stat.SF_NOUNLINK

The file may not be renamed or deleted.

stat.SF_SNAPSHOT

The file is a snapshot file.

See the *BSD or Mac OS systems man page chflags(2) for more information.

转载于:https://www.cnblogs.com/Security-Darren/p/4168329.html

相关文章:

Azure Neural TTS能让AI语音自然逼真到什么程度?

摘要:微软Azure Neural TTS让AI语音像真人一样富有感情,自然逼真。 Neural TTS(神经网络文本转语音)是微软Azure认知服务的强大语音合成功能,自推出以来,已被广泛应用于从语音助手、新闻阅读到有声读物创作…

ReentrantLock与synchronized

1、ReentrantLock 拥有Synchronized相同的并发性和内存语义,此外还多了 锁投票,定时锁等候和中断锁等候线程A和B都要获取对象O的锁定,假设A获取了对象O锁,B将等待A释放对O的锁定,如果使用 synchronized ,如…

EXT按钮事件

在EXT中,当我们要为按钮点击添加处理function的时候,可以看到一般人的实现分成2类:1.使用onClick: function xx()2.使用handler: function xx()完成后,我们会发现,无论用哪一种实现,再点击按钮时都能触发xx…

浅谈HTTP中Get与Post的区别

Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而H…

达摩院年终预测重磅出炉:AI for Science 高居榜首,2022 十大科技趋势!

整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)作为“一所探索科技未知的研究院”,阿里巴巴达摩院成立至今已经四年了。这四年来,达摩院秉持着“探索科技位置,以人类愿景为驱动力,开展基础科学和颠覆式…

TensorFlow——入门基础

TensorFlow原理: TensorFlow使用Graph来描述计算任务,图中的节点被称之为op.一个op可以接受0或多个tensor作为输入,也可产生0或多个tensor作为输出.任何一个Graph要想运行,都必须借助上下文Session.通过Session启动Graph,并将Graph中的op分发到CPU或GPU上,借助Sessi…

EXT iconCls说明

今天学习ext 看examples中的事例,其中有一个地方是这样写的: new ButtonPanel( Icon Only, [{ iconCls: add16 },{ iconCls: add24, scale: medium },{ …

25个好用到爆的一行 Python 代码,建议收藏

作者 | 欣一来源 | Pyhton爱好集中营在学习Python的过程当中,有很多复杂的任务其实只需要一行代码就可以解决,那么今天小编我就来给大家介绍实用的一行Python代码,希望对大家能够有所帮助。1.两个字典的合并x {a: 1, b: 2} y {c: 3, d: 4}将…

【工业串口和网络软件通讯平台(SuperIO)教程】七.二次开发服务驱动

SuperIO相关资料下载:http://pan.baidu.com/s/1pJ7lZWf 1.1 服务接口的作用 围绕着设备驱动模块采集的数据,根据需求提供多种应用服务,例如:数据上传服务、数据请求服务、4-20mA服务、短信服务、LED服务以及OPC服务等。保障数…

usermod命令,用户密码管理和mkpasswd命令

2019独角兽企业重金招聘Python工程师标准>>> usermod 设置扩展组 概念:更改用户属性的一个命令。 用法:usermod [选项] 后面跟你需要操作的内容 [用户名] 选项:-c, --comment 注释 GECOS 字段的新值-d, --home HO…

extjs关于jsonreader

在JavaScript中,JSON是一种非常重要的数据格式,key:value的形式比XML那种复杂的标签结构更容易理解,代码量也更小,很多人倾向于使用它作为EXT的数据交换格式。JsonReader支持分页,与JSON数据对应格式如下:t…

求逆元 - HNU 13412 Cookie Counter

Cookie Counter Problems Link: http://acm.hnu.cn/online/?actionproblem&typeshow&id13412&courseid0 Mean: 将N分为D份,每份不超过X,有多少种分法? analyse: 首先我们想到的是迭代,但是数据太大,…

IEEE 发布年终总结,AI 奇迹不再是故事

编译 | 禾木木 出品 | AI科技大本营(ID:rgznai100) 2021 年,人工智能奇迹不再只是故事! 人工智能正在迅速融入各行各业,IEEE Spectrum 总结了 2021 年 10 篇最受读者欢迎的 AI 文章,按时间排名,…

一则利用内核漏洞获取root权限的案例【转】

转自:https://blog.csdn.net/u014089131/article/details/73933649 目录(?)[-] 漏洞描述漏洞的影响范围漏洞曝光时间漏洞产生的原因漏洞的利用exploit代码分析kernel 最近出了一个新的本地提权安全漏洞CVE-2013-1763,影响范围比较广泛,ubunt…

Ext.data库

Ext.data 库主要包括以下几个类:Ext.data.Store >DataSetExt.data.Record >DataSet.RowExt.data.DataProxy >SqlConnectionExt.data.DataReader >SqlDataAdapter以下分别进行介绍:1.Ext.data.Record可以用来定义一行数据的格式,它有几个重要的属性和方法…

2021年最有用的数据清洗 Python 库

作者 | 周萝卜来源 | 萝卜大杂烩大多数调查表明,数据科学家和数据分析师需要花费 70-80% 的时间来清理和准备数据以进行分析。对于许多数据工作者来说,数据的清理和准备也往往是他们工作中最不喜欢的部分,因此他们将另外 20-30% 的时间花在抱…

组合与继承之重写方法和字段

为什么80%的码农都做不了架构师?>>> 接上篇blog,scala里的字段和方法属于相同的命名空间,这让字段可以重写无参数方法。例如,你可以通过改变ArrayElement类中contents的实现将其从一个方法变为一个字段,而…

20165334 四则运算阶段性总结(第二周)

四则运算阶段性总结(第二周) 结对对象 学号 :20165334 姓名 : 李天龙 担任角色 (驾驶员):李天龙 (副驾驶):陈国超 一、实验实现步骤 整数计算类分数计算类自动…

取消掉Transfer-Encoding:chunked

有时候,Web服务器生成HTTP Response是无法在Header就确定消息大小的,这时一般来说服务器将不会提供Content-Length的头信息,而采用Chunked编码动态的提供body内容的长度。进行Chunked编码传输的HTTP Response会在消息头部设置:Tra…

【LeetCode】142 - Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? Solution: Discuss上的分析:Suppose the first meet at step k,the length of the Cycle …

3000 字详解 Pandas 数据查询,建议收藏

作者 | 俊欣来源 | 关于数据分析与可视化今天小编来和大家说一说怎么从DataFrame数据集中筛选符合指定条件的数据,希望会对读者朋友有所帮助。导入数据集和模块我们先导入pandas模块,并且读取数据,代码如下import pandas as pd df pd.read_c…

stylus使用文档总结:内置方法+参数+条件+迭代+导入+继承

一、内置方法 返回各种颜色的比重(如red(color)等) 颜色函数是CSS预处里器中内置的颜色函数功能,这些功能可以对颜色值进行处理,例如颜色的变亮、变暗、渐变颜色等处理十分的方便。 lighten(color, 10%); /* 返回的颜色在color基础…

用 Python 制作酷炫的可视化大屏,特简单!

作者 | 小F来源 | 法纳斯特在数据时代,我们每个人既是数据的生产者,也是数据的使用者,然而初次获取和存储的原始数据杂乱无章、信息冗余、价值较低。要想数据达到生动有趣、让人一目了然、豁然开朗的效果,就需要借助数据可视化。以…

HTTP协议中的Tranfer-Encoding:chunked编码解析

当不能预先确定报文体的长度时,不可能在头中包含Content-Length域来指明报文体长度,此时就需要通过Transfer-Encoding域来确定报文体长度。通常情况下,Transfer-Encoding域的值应当为chunked,表明采用chunked编码方式来进行报文体的传输。chu…

[转] splice系列系统调用

关注splice系列系统调用(包括splice,tee和vmsplice)已经有一段时间了,开始的时候并未能领会splice的意义所在,致使得出了“splice系列系统调用不怎么实用”的错误结论。随着内核研究的深入,才逐渐懂得&…

嵌入式s5vp210裸机 KXTF9-2050(G-sensor)

1.KXTF9-2050简介 KXTF9-205是G-sensor的一种,G-sensor(Gravity sensor),重力传感器,又名加速度传感器(accelerometer),是能感知加速度大小的MEMS(微机电系统)传感器。使用I2C协议和…

JavaScript面向对象编程

自从有了Ajax这个概念,JavaScript作为Ajax的利器,其作用一路飙升。JavaScript最基本的使用,以及语法、浏览器对象等等东东在这里就不累赘了。把主要篇幅放在如何实现JavaScript的面向对象编程方面。1. 用JavaScript实现类 JavaScritpt没…

sublime text3 前端插件介绍

Emmet插件 Emmet插件可以说是使用Sublime Text进行前端开发必不可少的插件 它让编写HTML代码变得极其简单高效 基本用法:输入标签简写形式,然后按Tab键 关于Emmet的更多介绍,请查看官方文档 这份速查表,可以帮你快速记忆简写形式 …

如何使用 OpenCV Python 检测颜色

作者 | 小白来源 | 小白学视觉在这篇文章中,我们将看到如何使用 Python 中的 OpenCV 模块检测颜色,进入这个领域的第一步就是安装下面提到的模块。pip install opencv-python pip install numpy然后,导入模块。读取图像并使用 OpenCV 模块中的…

使用树形结构保存实体

阅读原文请访问我的博客BrightLoongs Blog之前在项目需要实现一个功能——将xml文件映射成实体,然后对映射的实体进行逻辑处理,最后保存到数据库中;由于xml结构的数据是结构化的数据,所以需要保证保存的数据具有正确的主外键关联。…