python目录操作之python遍历文件夹后将结果存储为xml


Posted in Python onJanuary 27, 2014

Linux服务器有CentOS、Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传到对应机器,在运行时修改参数即可。

Python操作文件和文件夹使用的是os库,下面的代码中主要用到了几个函数:

os.listdir:列出目录下的文件和文件夹
os.path.join:拼接得到一个文件/文件夹的全路径
os.path.isfile:判断是否是文件
os.path.splitext:从名称中取出一个子部分

下面是目录操作的代码

def search(folder, filter, allfile):
    folders = os.listdir(folder)
    for name in folders:
        curname = os.path.join(folder, name)
        isfile = os.path.isfile(curname)
        if isfile:
            ext = os.path.splitext(curname)[1]
            count = filter.count(ext)
            if count>0:
                cur = myfile()
                cur.name = curname
                allfile.append(cur)
        else:
            search(curname, filter, allfile)
    return allfile

在返回文件的各种信息时,使用自定义类allfile来保存文件的信息,在程序中只用到了文件的全路径,如果需要同时记录文件的大小、时间、类型等信息,可以仿照代码进行扩充。

class myfile:
    def __init__(self):
        self.name = ""

 得到存储文件信息的数组后,还可以将其另存成xml格式,下面是代码,在使用时,需要从Document中导入xml.dom.minidom

下面是保存为xml的代码

def generate(allfile, xml):
    doc = Document()
    root = doc.createElement("root")
    doc.appendChild(root)
    for myfile in allfile:
        file = doc.createElement("file")
        root.appendChild(file)
        name = doc.createElement("name")
        file.appendChild(name)
        namevalue = doc.createTextNode(myfile.name)
        name.appendChild(namevalue)
    print doc.toprettyxml(indent="")
    f = open(xml, 'a+')
    f.write(doc.toprettyxml(indent=""))
    f.close()

执行的代码如下
if __name__ == '__main__':
    folder = "/usr/local/apache/htdocs"
    filter = [".html",".htm",".php"]
    allfile = []
    allfile = search(folder, filter, allfile)
    len = len(allfile)
    print "found: " + str(len) + " files"
    xml = "folder.xml"
    generate(allfile, xml)

在Linux命令行状态下,执行Python filesearch.py,便可以生成名为folder.xml的文件。

如果要在Windows中运行该程序,需要把folder变量改成Windows下的格式,例如c:\\apache2\htdocs,然后执行c:\python25\python.exe filesearch.py(这里假设python的安装目录是c:\python25)

Python 相关文章推荐
Python collections模块实例讲解
Apr 07 Python
Python编程实现的图片识别功能示例
Aug 03 Python
Django如何实现内容缓存示例详解
Sep 24 Python
Python图像处理之图像的缩放、旋转与翻转实现方法示例
Jan 04 Python
对python中矩阵相加函数sum()的使用详解
Jan 28 Python
我用Python抓取了7000 多本电子书案例详解
Mar 25 Python
Python 中判断列表是否为空的方法
Nov 24 Python
基于matplotlib中ion()和ioff()的使用详解
Jun 16 Python
Python基于Webhook实现github自动化部署
Nov 28 Python
Python爬虫简单运用爬取代理IP的实现
Dec 01 Python
matplotlib事件处理基础(事件绑定、事件属性)
Feb 03 Python
Python anaconda安装库命令详解
Oct 16 Python
paramiko模块安装和使用(远程登录服务器)
Jan 27 #Python
python使用paramiko模块实现ssh远程登陆上传文件并执行
Jan 27 #Python
python list使用示例 list中找连续的数字
Jan 27 #Python
Python批量修改文件后缀的方法
Jan 26 #Python
使用cx_freeze把python打包exe示例
Jan 24 #Python
Python的函数嵌套的使用方法
Jan 24 #Python
下载安装setuptool和pip linux安装pip    
Jan 24 #Python
You might like
PHP编程中字符串处理的5个技巧小结
2007/11/13 PHP
php读取mysql中文数据出现乱码的解决方法
2013/08/16 PHP
php网站判断用户是否是手机访问的方法
2013/11/01 PHP
php函数serialize()与unserialize()用法实例
2014/11/06 PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
2017/05/04 PHP
intro.js 页面引导简单用法 分享
2013/08/06 Javascript
js使用for循环及if语句判断多个一样的name
2014/09/09 Javascript
jQuery获取对象简单实现方法小结
2014/10/30 Javascript
jquery中键盘事件小结
2016/02/24 Javascript
基于jquery实现轮播焦点图插件
2016/03/31 Javascript
JSON 必知必会 观后记
2016/10/27 Javascript
NodeJS遍历文件生产文件列表功能示例
2017/01/22 NodeJs
javascript 操作cookies详解及实例
2017/02/22 Javascript
js图片放大镜实例讲解(必看篇)
2017/07/17 Javascript
webpack+react+antd脚手架优化的方法
2018/04/02 Javascript
angularjs手动识别字符串中的换行符方法
2018/10/02 Javascript
vue计算属性computed、事件、监听器watch的使用讲解
2019/01/21 Javascript
[38:39]KG vs Mineski 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
Python3使用requests登录人人影视网站的方法
2016/05/11 Python
浅谈python迭代器
2017/11/08 Python
简单了解python模块概念
2018/01/11 Python
浅谈Python2之汉字编码为unicode的问题(即类似\xc3\xa4)
2019/08/12 Python
Python如何调用JS文件中的函数
2019/08/16 Python
使用python执行shell脚本 并动态传参 及subprocess的使用详解
2020/03/06 Python
Java Unsafe类实现原理及测试代码
2020/09/15 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
2021/02/20 Python
CSS3实现银灰色动画效果的导航菜单代码
2015/09/01 HTML / CSS
HTML5 LocalStorage 本地存储详细概括(多图)
2017/08/18 HTML / CSS
优秀毕业生自我鉴定
2014/02/11 职场文书
学生会主席演讲稿
2014/04/25 职场文书
承诺书格式
2014/06/03 职场文书
家长意见和建议怎么写
2015/06/04 职场文书
学生会干部任命书
2015/09/21 职场文书
golang 实现对Map进行键值自定义排序
2021/04/28 Golang
python 爬取哔哩哔哩up主信息和投稿视频
2021/06/07 Python