标签:
# -*- coding: utf-8 -*- import os, errno def fileName(path):#获取文件夹 str = ‘‘ for i in range(1,len(path.split(‘\\‘))): str+=path.split(‘\\‘)[i]+‘\\‘ return str def mkdir_p(path): #创建目录树 try: os.makedirs(path) except OSError as exc: # Python >2.5 (except OSError, exc: for Python <2.5) if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise def fileTraverse(filepath): #遍历filepath下所有文件,包括子目录 files = os.listdir(filepath) for fi in files: fi_d = os.path.join(filepath,fi) if os.path.isdir(fi_d): mkdir_p("E:\\"+fileName(fi_d)) #创建文件夹,文件夹目录树 fileTraverse(fi_d)#递归遍历 else: print os.path.join(filepath,fi_d) root = ‘F:\\目标2‘ root = root.decode(‘utf-8‘)#目录名中有中文,需要decode fileTraverse(root)
标签:
原文地址:http://www.cnblogs.com/XDJjy/p/5288672.html