【Python遍历技巧】教程文章相关的互联网学习教程文章

Python遍历目录并批量更换文件名和目录名的方法

本文实例讲述了Python遍历目录并批量更换文件名和目录名的方法。分享给大家供大家参考,具体如下:#encoding=utf-8 #author: walker #date: 2014-03-07 #summary: 深度遍历指定目录,并将子目录和文件名改为小写 #注意,此程序只针对windows,windows下文件(夹)名不区分大小写 import os import os.path import shutil #读入指定目录并转换为绝对路径 rootdir = raw_input(root dir:\n) rootdir = os.path.abspath(rootdir) prin...

Python简单遍历字典及删除元素的方法

本文实例讲述了Python简单遍历字典及删除元素的方法。分享给大家供大家参考,具体如下:这种方式是一定有问题的:d = {a:1, b:2, c:3} for key in d:d.pop(key)会报这个错误:RuntimeError: dictionary changed size during iteration这种方式Python2可行,Python3还是报上面这个错误。d = {a:1, b:2, c:3} for key in d.keys():d.pop(key)Python3报错的原因是keys()函数返回的是dict_keys而不是list。Python3的可行方式如下:d = ...

Python实现递归遍历文件夹并删除文件

思路:遍历文件夹下面的文件夹如果文件夹名称等于".svn",则修改文件夹的属性(因为".svn"的文件都是只读的,你不能直接删除)删除此文件夹如果文件夹名称不等于".svn",则递归上面的方法Python的实现代码import os import shutil import os.path import stat rootdir="F:\\work\\Test" for parent,dirnames,filenames in os.walk(rootdir): #遍历文件夹下面的所有文件夹 for dirname in dirnames: if dirname==.svn:strfilepath=pa...

Python用zip函数同时遍历多个迭代器示例详解

前言本文主要介绍的是Python如何使用zip函数同时遍历多个迭代器,文中的版本为Python3,zip函数是Python内置的函数。下面话不多说,来看详细的内容。应用举例>>> list1 = [a, b, c, d] >>> list2 = [apple, boy, cat, dog] >>> for x, y in zip(list1, list2):print(x, is, y) # 输出 a is apple b is boy c is cat d is dog 这样就很简洁地实现了同时遍历两个列表,very pythonic!!!原理说明Python3中的zip函数可以把两个或者...

python实现二叉树的中序遍历

#!/usr/bin/env python# coding=utf-8# inorderBL.pyimport stdinInputdef inorder(arrays,arraysize,currentP): if(2*currentP+1<arraysize): inorder(arrays,arraysize,2*currentP+1) print arrays[currentP] if(2*currentP+2<arraysize): inorder(arrays,arraysize,2*currentP+2)if __name__==__main__: stdinInput.stdinInput() inorder(stdinInput.intsortArrays,len(stdinInput.intsortArrays)...

python图自身遍历及弱引用使用

在【python 标准库】中看到的一段代码,非常有帮助:def all_nodes(self):yield selfn = self.otherwhile n and n.name != self.name:yield nn = n.otherif n is self:yield nreturn 首尾的2处yield均只返回一次,作为循环图的起点、终点,而n作为图可能的节点,每次在next调用中均返回next节点利用这个迭代器,就可以轻松打印出图的结构:def __str__(self): return ->.join((n.name for n in self.all_nodes()))Graph:on...

Python遍历目录中的所有文件的方法

os.walk生成器 os.walk(PATH), PATH是个文件夹路径,当然可以用.或者../这样啦. 返回的是个三元元组为元素的列表, 每个元素代表了一个文件夹下的内容.第一个就是当前文件夹下内容. 返回的三元元组代表(该工作文件夹, 该文件夹下的文件夹的列表, 该文件夹下文件的列表). 所以, 获得所有子文件夹, 就是(d代表这三元元组):os.path.join(d[0],d[1]); 获得所有子文件, 就是:os.path.join(d[0],d[2]); 以下例子使用了两套循环, 遍历后得到...

python二叉树遍历的实现方法

代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- class TreeNode(object): def __init__(self,data=0,left=0,right=0): self.data = data self.left = left self.right = right class BTree(object): def __init__(self,root=0): self.root = rootdef is_empty(self): if self.root is 0: return True else: return Falsedef preOrder(self,treenode):...

python使用os模块的os.walk遍历文件夹示例

代码如下:#-*- coding:utf-8 -*- import os if __name__ == __main__: try: traval and list all files and all dirs for root, dirs, files in os.walk(D: + os.sep + Python27): print -------------------directory < + root + > --------------------------for d in dirs: print d for f in files: print f except OSError, e: print os.strerror(e.errno)

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

Linux服务器有CentOS、Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传到对应机器,在运行时修改参数即可。 Python操作文件和文件夹使用的是os库,下面的代码中主要用到了几个函数: os.listdir:列出目录下的文件和文件夹os.path.join:拼接得到一个文件/文件夹的全路径os.path.isfile:判断是否是文件os.path.splitext:从名称中取出一个子部分 下面...

python遍历文件夹并删除特定格式文件的示例

代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- import os def del_files(path): for root , dirs, files in os.walk(path): for name in files: if name.endswith(".tmp"): os.remove(os.path.join(root, name)) print ("Delete File: " + os.path.join(root, name)) # testif __name__ == "__main__": path = /tmp del_files(path)

python实现dict版图遍历示例

代码如下:#_*_coding:utf_8_import sysimport os class Graph(): def __init__(self, V, E): self.V = V self.E = E self.visited = [] self.dict = {} self.fd = open("input.txt") def initGraph(self): self.visited = [0 for i in range(self.V+1)] for i in range(self.E): f, t = map(int, self.fd.readline().split()) #f, t = map(int, sys...

python数据结构之二叉树的遍历实例

遍历方案  从二叉树的递归定义可知,一棵非空的二叉树由根结点及左、右子树这三个基本部分组成。因此,在任一给定结点上,可以按某种次序执行三个操作:  1).访问结点本身(N)  2).遍历该结点的左子树(L)  3).遍历该结点的右子树(R) 有次序:  NLR、LNR、LRN 遍历的命名根据访问结点操作发生位置命名:NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。LNR:中序遍历(...

python两种遍历字典(dict)的方法比较

python以其优美的语法和方便的内置数据结构,赢得了不少程序员的亲睐。其中有个很有用的数据结构,就是字典(dict),使用非常简单。说到遍历一个dict结构,我想大多数人都会想到 for key in dictobj 的方法,确实这个方法在大多数情况下都是适用的。但是并不是完全安全,请看下面这个例子: 代码如下:#这里初始化一个dict>>> d = {a:1, b:0, c:1, d:0}#本意是遍历dict,发现元素的值是0的话,就删掉>>> for k in d:... if d[k] ...

Python中文件遍历的两种方法

关于Python的文件遍历,大概有两种方法,一种是较为便利的os.walk(),还有一种是利用os.listdir()递归遍历。 方法一:利用os.walk os.walk可以自顶向下或者自底向上遍历整个文件树,然后返回一个含有3个元素的tuple,(dirpath, dirnames, filenames),要注意的是,os.walk()会返回一个generater,所以调用的时候一定要放到for循环中。 代码如下:import osdef walk_dir(dirname): for root,dirs,files in os.walk(dirname): for f ...