欢迎来到山村网

Python遍历指定文件及文件夹的方法

2019-03-02 12:35:32浏览:566 来源:山村网   
核心摘要:  这篇文章主要介绍了Python遍历指定文件及文件夹的方法,对比两种实现技巧分析了Python遍历文件及文件夹的方法,需要的朋友可以

  这篇文章主要介绍了Python遍历指定文件及文件夹的方法,对比两种实现技巧分析了Python遍历文件及文件夹的方法,需要的朋友可以参考下

  初次编写:

  ?

1 2 3 4 5 6 7 import os def searchdir(arg,dirname,names): for filespath in names: open ('c:test.txt','a').write('%srn'%(os.path.join(dirname,filespath))) if __name__=="__main__": paths="g:" os.path.walk(paths,searchdir,())

  做了修改,添加了文件属性

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # -*- coding: cp936 -*- import os,time #将文件属性中的时间改为‘2011-1-12 00:00:00格式' def formattime(localtime): endtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(localtime)) return endtime def searchdir(arg,dirname,names): for filespath in names: #得到文件路径 fullpath=os.path.join(dirname,filespath) #得到文件属性 statinfo=os.stat(fullpath) #文件大小 sizefile=statinfo.st_size #创建时间 creattime=formattime(statinfo.st_ctime) #修改时间 maketime=formattime(statinfo.st_mtime) #浏览时间 readtime=formattime(statinfo.st_atime) #判断是文件夹还是文件 if os.path.isdir(fullpath): filestat='DIR' else: filestat='FILE' open ('c:test.txt','a').write('【%s】路径:%s 文件大小(B):%s 创建时间:%s 修改时间:%s 浏览时间:%srn'%(filestat,fullpath,sizefile,creattime,maketime,readtime)) if __name__=="__main__": paths="g:" os.path.walk(paths,searchdir,())

  希望本文所述对大家的Python程序设计有所帮助。

(责任编辑:豆豆)
下一篇:

python使用xlrd实现检索excel中某列含有指定字符串记录的方法

上一篇:

Python使用chardet判断字符编码

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com