Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120951
  • 博文数量: 35
  • 博客积分: 1672
  • 博客等级: 上尉
  • 技术积分: 412
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-19 15:16
文章分类

全部博文(35)

文章存档

2012年(5)

2011年(9)

2010年(21)

我的朋友

分类:

2010-07-05 19:00:48

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)

阅读(533) | 评论(0) | 转发(0) |
0

上一篇:Linux SCP文件拷贝

下一篇:date

给主人留下些什么吧!~~