最近这几天,一直在学习python,发现python在脚本处理方面有很多的便捷性,这点要比java要好,但是文档找起来并不是那么方便,刚上手,有点生疏。
以下代码是python用来处理文件夹遍历的脚本(OS : ubuntu 11.04 python:2.7)
#coding:utf-8 #iterator folder use python import os CODING = 'gb2312' def iteratorFolder(fall,fileName): files = os.listdir(fileName) sep = os.path.sep for name in files: fullname = fileName + sep + name if os.path.isdir(fullname): iteratorFolder(fall,fullname) else : pass #fall.append(unicode(fullname,CODING)) #fall.append(fullname) fall.append(unicode(fullname,CODING)) #fall.append(fullname) return fall def main(): fall = [] fileName = '/home/gavin/projects/email/mail_project_doc' file = open('log.log','w') fall = iteratorFolder(fall,fileName) for f in fall: print f print f.encode(CODING) file.writelines(f+'\n') file.close() #print file.name #print len(fall) if __name__ == '__main__': main()
相对比的java代码如下:
package com.sohu.test; import java.io.*; import java.util.*; public class IteratorFolder { public static List<String> iteratorFolder(List<String> list,String fileName) throws UnsupportedEncodingException{ File file = new File(fileName); File[] files = file.listFiles(); String separator = File.separator; for(int i = 0 ; i < files.length ; i ++){ File f = files[i]; String fname = f.getName(); String fullName = fileName + separator + fname; if(f.isDirectory()){ iteratorFolder(list,fullName); } list.add(fullName); } return list; } public static void main(String[] args)throws Exception { // check file String name = "/home/gavin/projects/email/test"; List<String> list = new ArrayList<String>(); list = iteratorFolder(list,name); File file = new File("log_java.log"); FileOutputStream fos = new FileOutputStream(file); PrintWriter out = new PrintWriter(new OutputStreamWriter(fos)); for(String fullName : list){ out.write(fullName+"\n"); } out.close(); fos.close(); System.out.println(file.getAbsolutePath()); System.out.println(list.size()); } }
感觉还是python在文件处理上比java要方便些!在跟同事看了之后,给了一个更简单的处理方式os.walk,这个可以直接遍历文件夹目录做处理,如:
#file = open(fileName,'r') files = os.walk(fileName) for f in files: for ff in f: print ff
可参考os.walk用法:http://www.cnblogs.com/phonefans/archive/2008/10/15/1311707.html
后续再做python的脚本优化!(文件校验判断,文件的参数传入等)