python开发_stat
当我们使用os.stat(path)获取一个文件(夹)信息的时候,
os.stat(path)本身返回的是一个元组如:
nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336,
st_mtime=1376534141, st_ctime=1376373336)
在这个元组中,包含了10个属性:
st_mode -- protection bits(模式)
st_ino -- inode number(索引号)
st_dev -- device(设备)
st_nlink -- number of hard links(硬链接号)
st_uid -- user id of owner(用户id)
st_gid -- group id of owner (组id)
st_size -- size of file,in bytes (大小)
st_atime -- time of most recent access expressed in seconds (访问时间)
st_mtime -- time of most recent content modificatin expressed in seconds (修改时间)
st_ctime -- platform dependent;time of most recent metadata change on Unix,
or the teime of creation on Windows,expressed in senconds (根据不同操作系统而定)
#############################################################
而stat在这里起到什么作用呢?
类似于java中定义的一些常量:
如:
os.stat(path).st_size
os.stat(path)[stat.ST_SIZE]
这两种表示方法是一样的。
下面是我做的demo:
运行效果:

==============================================
代码部分:
==============================================
#python stat
'''
当我们使用os.stat(path)获取一个文件(夹)信息的时候,
os.stat(path)本身返回的是一个元组如: nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336,
st_mtime=1376534141, st_ctime=1376373336) 在这个元组中,包含了10个属性:
st_mode -- protection bits(模式)
st_ino -- inode number(索引号)
st_dev -- device(设备)
st_nlink -- number of hard links(硬链接号)
st_uid -- user id of owner(用户id)
st_gid -- group id of owner (组id)
st_size -- size of file,in bytes (大小)
st_atime -- time of most recent access expressed in seconds (访问时间)
st_mtime -- time of most recent content modificatin expressed in seconds (修改时间)
st_ctime -- platform dependent;time of most recent metadata change on Unix,
or the teime of creation on Windows,expressed in senconds (根据不同操作系统而定) #############################################################
而stat在这里起到什么作用呢?
类似于java中定义的一些常量:
如:
os.stat(path).st_size
os.stat(path)[stat.ST_SIZE]
这两种表示方法是一样的。
'''
import os
import time
import stat def get_file_stat(path):
'''获取一个文件(夹)信息,该信息将以一个元组的形式返回'''
if os.path.exists(path):
return os.stat(path)
else:
print('the path [{}] is not exist!'.format(path)) def print_info(file_stat):
'''打印信息'''
if file_stat != None:
file_info = {
'Size' : file_stat [ stat.ST_SIZE ], #获取文件大小
'LastModified' : time.ctime( file_stat [ stat.ST_MTIME ] ), #获取文件最后修改时间
'LastAccessed' : time.ctime( file_stat [ stat.ST_ATIME ] ), #获取文件最后访问时间
'CreationTime' : time.ctime( file_stat [ stat.ST_CTIME ] ), #获取文件创建时间
'Mode' : file_stat [ stat.ST_MODE ], #获取文件的模式
'Device' : file_stat [stat.ST_DEV], #设备
'UserID' : file_stat [stat.ST_UID],
'GroupID' : file_stat [stat.ST_GID]
}
for key in file_info:
print('{} : {}'.format(key, file_info[key]))
else:
print('the stat is None!') def main():
path_dir = 'c:\\Download'
path_file = 'c:\\test.html'
print('目录信息:')
file_stat = get_file_stat(path_dir)
print_info(file_stat)
print('#' * 50)
print('文件信息:')
file_stat = get_file_stat(path_file)
print_info(file_stat) if __name__ == '__main__':
main()
python开发_stat的更多相关文章
- python开发环境搭建
虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
- Python开发工具PyCharm个性化设置(图解)
Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...
- Python黑帽编程1.2 基于VS Code构建Python开发环境
Python黑帽编程1.2 基于VS Code构建Python开发环境 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...
- Eclipse中Python开发环境搭建
Eclipse中Python开发环境搭建 目 录 1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...
- Python开发:环境搭建(python3、PyCharm)
Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))
- Python 开发轻量级爬虫08
Python 开发轻量级爬虫 (imooc总结08--爬虫实例--分析目标) 怎么开发一个爬虫?开发一个爬虫包含哪些步骤呢? 1.确定要抓取得目标,即抓取哪些网站的哪些网页的哪部分数据. 本实例确定抓 ...
- Python 开发轻量级爬虫07
Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...
- Python 开发轻量级爬虫06
Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...
随机推荐
- Spring boot 入门二:Spring Boot配置文件详解
一.自定义属性 当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties.同时也支持ym ...
- css 相对单位rem详解
CSS3新增了一个相对单位rem(root em,根em),这个单位引起了广泛关注.这个单位与em有什么区别呢?区别在于使用rem为元素设定字体大小时,仍然是相对大小,但相对的只是HTML根元素. ...
- HDU4418 Time travel(期望dp 高斯消元)
题意 题目链接 Sol mdzz这题真的太恶心了.. 首先不难看出这就是个高斯消元解方程的板子题 \(f[x] = \sum_{i = 1}^n f[to(x + i)] * p[i] + ave\) ...
- linux 光盘yum源搭建
1.挂载光盘 2.进入 /etc/yum.repos.d 目录,修改其它配置文件后缀名 mv CentOS-Base.repo CentOS-Base.repo.bakmv CentOS-CR.rep ...
- qt cmake
写在前面的话:强烈建议优先阅读Qt官网文档对cmake的使用介绍——CMake Manual 前言我去年用clion写Qt的时候,找了很多教程,也没有什么让我觉得很满意的.后来自己摸索,构建了一个我自 ...
- commonjs详解
marked here a well written artical http://javascript.ruanyifeng.com/nodejs/module.html
- 第六章 函数、谓词、CASE表达式 6-1 各种各样的函数
一.函数的种类 算术函数 字符串函数 日期函数 转换函数 聚合函数 二.算术函数 + - * / 1.ABS——绝对值 ABS(数值) 绝对值 absolute value ,不考虑数值的符号 ...
- Vue2学习笔记:过渡效果css
过渡效果 Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加 entering/leaving 过渡 <!DOCTYPE html> <html ...
- RESET MASTER和RESET SLAVE使用场景和说明
[前言]在配置主从的时候经常会用到这两个语句,刚开始的时候还不清楚这两个语句的使用特性和使用场景. 经过测试整理了以下文档,希望能对大家有所帮助: [一]RESET MASTER参数 功能说明:删除所 ...
- 转:MVC框架
MVC框架是什么 MVC (Modal View Controler)本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离,从而使 ...