首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python_递归遍历资料

2012-12-22 
Python__递归遍历文件http://zhangjunhd.blog.51cto.com/113473/453857http://www.cnblogs.com/xuxm2007/a

Python__递归遍历文件
http://zhangjunhd.blog.51cto.com/113473/453857
http://www.cnblogs.com/xuxm2007/archive/2010/08/09/1795504.html
http://www.cnblogs.com/phonefans/archive/2008/10/15/1311707.html

#coding=gbk'''os.walk(dir) 是三列的数组'''import osdir = os.getcwd()for cons in os.walk(dir):    print cons#根目录、所有目录、目录下的文件for root, dirs, files in os.walk(dir):    for name in files:        print os.path.join(root, name)

def getAllFile (dir, list):    for root, dirs, files in os.walk(dir):        for name in files:            list.append( os.path.join(root, name) )    return len(list)


file.read()  读全部
file.read(N) N个字节
file.readline() 
file.readlines() return a list

file.write(...)

file.seek(0)
file.next()
file.close()

#!/usr/bin/python

import osimport shutil,string def subString(str, sub):    return sub in strdir = '/Users/xserver/Desktop/new_res/'for root, dirs, files in os.walk(dir):      for name in files:        path = os.path.join(root, name)         if subString(path, '.jpg'):            path2 = path.replace('.jpg','.png')            print path            shutil.move(path, path2);

热点排行