当我们使用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的更多相关文章

  1. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

  2. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

  3. Python开发工具PyCharm个性化设置(图解)

    Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...

  4. Python黑帽编程1.2 基于VS Code构建Python开发环境

    Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...

  5. Eclipse中Python开发环境搭建

    Eclipse中Python开发环境搭建  目 录  1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...

  6. Python开发:环境搭建(python3、PyCharm)

    Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))

  7. Python 开发轻量级爬虫08

    Python 开发轻量级爬虫 (imooc总结08--爬虫实例--分析目标) 怎么开发一个爬虫?开发一个爬虫包含哪些步骤呢? 1.确定要抓取得目标,即抓取哪些网站的哪些网页的哪部分数据. 本实例确定抓 ...

  8. Python 开发轻量级爬虫07

    Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...

  9. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

随机推荐

  1. dubbo直连代码示例

    我们都知道dubbo是个分布式的RPC工具,等以后有时间能力允许的话,会好好写下dubbo,当在测试环境我们联调或想指定机器执行之时,是不需要ZK这类调度工具的,当然dubbo也提供了配置的解决方案, ...

  2. LeetCode CombinationSum II

    class Solution { public: vector<vector<int> > combinationSum2(vector<int> &num ...

  3. Drupal8学习之路--官网文档碎碎记--主题篇

    主要记录一些琐碎的知识点. 1.“In Drupal 8 drupal_add_css(), drupal_add_js() and drupal_add_library()were removed ...

  4. 关于session销毁的问题,invalidate() 和removeAttribute()

    request.getSession().invalidate(); 销毁当前会话域中的所有属性 request.getSession().removeAttribute("username ...

  5. 基于Maven + SSM (Spring、SpringMVC、Mybatis)构建一个简单的测试项目

    最近在公司实习期间的培训交流中有机会接触到SSM,然后自己花费1周的时间投入学习.谈不上深刻理解其中原理,所以没有涉及理论知识,只是浅层次的学习如何使用,在此将学习过程记录整理出来,一方面自己备用:另 ...

  6. flutter实现(OutlineButton)线框按钮

    在flutter的控件里 常用按钮有:FlatButton,RaisedButton,FloatingActionButton,OutlineButton. FlatButton是扁平的,没有阴影的. ...

  7. git 错误error: failed to push some refs to

    今天使用VSCODE 学习node.js,  想在git上push代码 于是在git上建立了一个私有的长裤, 连接后push代码时提示如下错误: error: failed to push some ...

  8. .NET版UEditor报请求后台配置项http错误,上传功能无法使用的错误解决

    在配置UEditor的时候,总是报请求后台配置项http错误,上传功能将不能正常使用!,上传图片等功能都无法使用.折磨了一下午,逐步调试发现了错误原因:

  9. SQL Server 索引知识-概念

    概念篇 索引概念(index concept) 一种表或视图中相关的B-tree的数据结构.索引键列由一列或多列组成.可拥有包含性列(sql2005).用于提升Sql Server 查找相关数据行效率 ...

  10. CentOS随笔 - 5.CentOS7安装Sql Server 2017

    前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 开发环境嘛, 作为.Net系Sql Server那是必备的. 听过Sql server可以安装在Linux上了 ...