1.本python程序,对一堆组织很乱的java文件,从package中提取出路径,并将该java文件copy到路径下
2.遍历目录时使用递归
#!/usr/bin/python
import os
import sys
import codecs
import shutil
#srcpath=raw_input('Input java src folder')
#dstpath=raw_input('Input java dst folder')
srcpath='/home/IWSVA/IWSS/src/AdminUI/javaclass/src'
dstpath='/home/python/file'
package='com.trend.iwss'
def dirjavafile(path):
fd=open(path)
isPackage=False
for line in fd:
if line[:3]==codecs.BOM_UTF8:
line=line[3:]
line.strip()
if line.startswith('package'):
isPackage=True
break
fd.close()
if isPackage:
beg=line.find(package)
end=line.find(';')
javapath=line[beg:end].strip().replace('.', os.sep)
dstdir=dstpath + os.sep + javapath
if not os.path.exists(dstdir):
os.makedirs(dstdir)
dstfile=dstdir + os.sep + os.path.basename(path)
print dstfile
shutil.copyfile(path, dstfile)
'''show all the jav file
and pass the java file path to the dirjavafile function'''
def allfiles(path):
for i in os.listdir(path):
tmppath=path+os.sep+i
if os.path.isdir(tmppath):
allfiles(tmppath)
else:
dirjavafile(tmppath)
allfiles(srcpath)
阅读(569) | 评论(0) | 转发(0) |