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--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...
随机推荐
- Code Signal_练习题_almostIncreasingSequence
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly incr ...
- Bootstrap学习笔记01
1.Make Images Mobile Responsive 用处: 使图片适配你的页面宽度. 操作: 给图片添加 .img-responsive class属性. <img src= ...
- css之背景(background)家族
背景(background)是css中很重要的一部分,也是css的基础知道之一,现在来回顾css2中5个属性与css3中新增的3个属性和2个功能. CSS2_背景(background)前传 家族成员 ...
- react组件直接在document上添加事件
demo:比如组件里有个div写的框框,点击document body的背景色变红,点击div写的框框没效果 componentDidMount(){ document.onclick = this. ...
- CSS代码缩写
盒模型代码简写 还记得在讲盒模型时外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左.具体应用在margin和paddin ...
- 基于goahead 的固件程序分析
# 前言 本文由 本人 首发于 先知安全技术社区: https://xz.aliyun.com/u/5274 最近在分析 dlink 的一个固件时遇到了用 goahead 开发的 web 服务.本文以 ...
- 导出PDF乱码
客户问题: 客户环境 LINUX系统weblogic10.3.0.0 用weblogic自带 JDK160_05 导出PDF中文字体全是口 解决方法: 客户的说他们的测试服务器和生产服务器环境是 ...
- python 字符串格式化符号含义及注释
字符串格式化符号含义 符号 说明 %C 格式化字符及其ASCII码 %S 格式化字符串 %d 格式化整数 %o 格式化无符号八进制数 %x 格式化无符号十六进制数 %X 格式化无符号十六进制数(大写) ...
- Debian出现in the drive ‘/media/cdrom/’ and press enter解决办法
没有光盘源解决打开/etc/apt/sources.list文件,注释掉cdrom那一行,然后再执行apt-get update更新下deb仓库这样以后再使用apt-get安装时就不会再搜寻cdrom ...
- [UI] 精美UI界面欣赏[9]
精美UI界面欣赏[9]