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程序设计有所帮助。

时间: 2024-08-03 14:36:52

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

PHP递归遍历指定目录的文件并统计文件数量的方法_php技巧

本文实例讲述了PHP递归遍历指定目录的文件并统计文件数量的方法.分享给大家供大家参考.具体实现方法如下: <?php //递归函数实现遍历指定文件下的目录与文件数量 function total($dirname,&$dirnum,&$filenum){ $dir=opendir($dirname); echo readdir($dir)."<br>"; //读取当前目录文件 echo readdir($dir)."<br>&qu

PHP递归遍历指定目录的文件并统计文件数量的方法

 这篇文章主要介绍了PHP递归遍历指定目录的文件并统计文件数量的方法,涉及php文件及目录操作的技巧,非常具有实用价值,需要的朋友可以参考下     本文实例讲述了PHP递归遍历指定目录的文件并统计文件数量的方法.分享给大家供大家参考.具体实现方法如下: ? 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 <?php //递归函数实现遍历指定文件下的目录与文件数量 function total($di

用Python遍历C盘dll文件的方法

  这篇文章主要介绍了用Python遍历C盘dll文件的方法,用fnmatch模块实现起来非常简单,需要的朋友可以参考下 python 的fnmatch 还真是省心,相比于 java 中的FilenameFilter ,真是好太多了,你完成不需要去实现什么接口. fnmatch 配合 os.walk() 或者 os.listdir() ,你能做的事太多了,而且用起来相当 easy. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # codin

python对指定目录下文件进行批量重命名的方法_python

本文实例讲述了python对指定目录下文件进行批量重命名的方法.分享给大家供大家参考.具体如下: 这段python代码可对c:\temp目录下的所有文件名为"scroll_1"文件替换为"scroll_00" import os path = 'c:\\temp' for file in os.listdir(path): if os.path.isfile(os.path.join(path,file))==True: newname = file.replace

python获取指定网页上所有超链接的方法_python

本文实例讲述了python获取指定网页上所有超链接的方法.分享给大家供大家参考.具体如下: 这段python代码通过urllib2抓取网页,然后通过简单的正则表达式分析网页上的全部url地址 import urllib2 import re #connect to a URL website = urllib2.urlopen(url) #read html code html = website.read() #use re.findall to get all the links links

python遍历 truple list dictionary的几种方法总结_python

实例如下: def TestDic1(): dict2 ={'aa':222,11:222} for val in dict2: print val def TestDic2(): dict2 ={'aa':222,11:222} for (key,val) in dict2.items(): print key,":",val def TestList1(): list=[1,2,3,4,5,3,2,'ada','fs3'] for i in range(len(list)): pr

python遍历 truple list dictionary的几种方法

def TestDic1(): dict2 ={'aa':222,11:222} for val in dict2: print val def TestDic2(): dict2 ={'aa':222,11:222} for (key,val) in dict2.items(): print key,":",val def TestList1(): list=[1,2,3,4,5,3,2,'ada','fs3'] for i in range(len(list)): print li

python 遍历文件中的关键词

问题描述 python 遍历文件中的关键词 用python遍历指定文件夹下的所有文件内容, 输入一个中文关键词,得到所有txt文件中关键词出现的次数 解决方案 python 文件遍历python 遍历文件夹及文件python遍历文件夹和文件 解决方案二: 用listdir等读取文件内容然后分词 统计个数.再根据输入返回结果

中文乱码-读取python遍历中文目录得到的文件路径报错

问题描述 读取python遍历中文目录得到的文件路径报错 各位朋友们好,我是一名python新手,现在遇到一个问题,恳请各位朋友们能指点一下我. 我的环境是:ubuntu14.04系统,python2.7 用python遍历一个目录,目录层次结构如下: 这是主目录: /home/chaoma/superboard/mydata/input/production 主目录下的目录层次结构如下 --超级画板教学资源_全处理_772 -- Z+Z资源库 --初中数学资源库 --1有理数 ---1.1数的