python获取文件信息

# -*- coding: UTF8 -*-

import time

import datetime

import os


1、  '''把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12'''

def TimeStampToTime(timestamp):

timeStruct = time.localtime(timestamp)

return time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)


2、  '''获取文件的大小,结果保留两位小数,单位为MB'''

def get_FileSize(filePath):

filePath = unicode(filePath,'utf8')

fsize = os.path.getsize(filePath)

fsize = fsize/float(1024*1024)

return round(fsize,2)

3、  '''获取文件的访问时间'''

def get_FileAccessTime(filePath):

filePath = unicode(filePath,'utf8')

t = os.path.getatime(filePath)

return TimeStampToTime(t)

4、  '''获取文件的创建时间'''

def get_FileCreateTime(filePath):

filePath = unicode(filePath,'utf8')

t = os.path.getctime(filePath)

return TimeStampToTime(t)

5、  '''获取文件的修改时间'''

def get_FileModifyTime(filePath):

filePath = unicode(filePath,'utf8')

t = os.path.getmtime(filePath)

return TimeStampToTime(t)




os.path.abspath(path) #返回绝对路径

os.path.basename(path) #返回文件名

os.path.commonprefix(list) #返回多个路径中,所有path共有的最长的路径。

os.path.dirname(path) #返回文件路径

os.path.exists(path)  #路径存在则返回True,路径损坏返回False

os.path.lexists  #路径存在则返回True,路径损坏也返回True

os.path.expanduser(path)  #把path中包含的"~"和"~user"转换成用户目录

os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name”和”${name}”

os.path.getatime(path)  #返回最后一次进入此path的时间。

os.path.getmtime(path)  #返回在此path下最后一次修改的时间。

os.path.getctime(path)  #返回path的大小

os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误

os.path.isabs(path)  #判断是否为绝对路径

os.path.isfile(path)  #判断路径是否为文件

os.path.isdir(path)  #判断路径是否为目录

os.path.islink(path)  #判断路径是否为链接

os.path.ismount(path)  #判断路径是否为挂载点()

os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径

os.path.normcase(path)  #转换path的大小写和斜杠

os.path.normpath(path)  #规范path字符串形式

os.path.realpath(path)  #返回path的真实路径

os.path.relpath(path[, start])  #从start开始计算相对路径

os.path.samefile(path1, path2)  #判断目录或文件是否相同

os.path.sameopenfile(fp1, fp2)  #判断fp1和fp2是否指向同一文件

os.path.samestat(stat1, stat2)  #判断stat tuple stat1和stat2是否指向同一个文件

os.path.split(path)  #把路径分割成dirname和basename,返回一个元组

os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组

os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组

os.path.splitunc(path)  #把路径分割为加载点与文件

os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数

os.path.supports_unicode_filenames  #设置是否支持unicode路径名

下面给出一种遍历的实现:

1rootdir ='F:\data

'2list = os.listdir(rootdir)#列出文件夹下所有的目录与文件

3foriin range(0,len(list)):

4path = os.path.join(rootdir,list[i])

5if os.path.isfile(path):

6#你想对文件的操作

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • os.path.abspath(path) #返回绝对路径,和realpath有区别,realpath是返回文件真...
    jova_y阅读 825评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,958评论 19 139
  • 想入门python图像处理,一些涉及到保存路径的操作,split/splitext/dirname/basenam...
    SunFlowWind阅读 874评论 0 0
  • 你在我梦里徘徊 醒来给我的是泪流满面 多么想在寒风中 拥抱你柔软的身体 多么想在柳絮飞扬下 牵起你纤细的手 多么想...
    b6eeafdbf931阅读 167评论 0 0
  • 我心目中的生活,是应该充满着自由、爱意、融洽、小惊喜的。在这样的生活中,工作时间要自由,家人之间要平等有爱,朋友之...
    等等我可好阅读 209评论 0 2