优化简单的Python文件匹配&替换脚本

时间:2012-03-12 17:27:11

标签: python

只是想知道是否有人建议如何优化我的简单但缓慢的文件替换脚本:

def switchFiles(args):
for root1, dirs1, files1 in os.walk(args.folder):
    for f1 in files1:
        for root2, dirs2, files2 in os.walk(args.database):
            for f2 in files2:
                if fnmatch.fnmatch(f1, f2):
                    command = 'cp '+os.path.join(root1, f1)+' '+os.path.join(root2, f2)
                    print(command)
                    os.system(command)

谢谢!

2 个答案:

答案 0 :(得分:1)

这是一个清理代码:

def switchFiles(args):
    pairs = []
    for root1, dirs1, files1 in os.walk(args.folder):
        for f1 in files1:
            for root2, dirs2, files2 in os.walk(args.database):
                for f2 in files2:
                    if f1 == f2:
                        pairs.append(os.path.join(root1, f1), os.path.join(root2, f2))
    for src, dst in pairs:
        shutil.copyfile(src, dst)

如果args.folder和args.database是分开的(不是subdir),并且其dir中的所有文件名都是唯一的,那么你可以这样做:

def switchFiles(args):
    f, d = {}, {}
    for root1, dirs1, files1 in os.walk(args.folder):
        for f1 in files1:
            f[f1] = os.path.join(root1, f1)
    for root2, dirs2, files2 in os.walk(args.database):
        for f2 in files2:
            d[f2] = os.path.join(root2, f2)
    ns = set(f.keys()) & set(d.keys())
    for n in ns:
        shutil.copyfile(f[n], d[n])

答案 1 :(得分:0)

如果args.folder只有很少的文件,我认为这个会更快。

def switchFiles(args):
    srclst = {}
    for root, dirs, files in os.walk(args.folder):
        rootp = (root,)
        for filename in files:
            srclst[filename] = rootp
    for root, dirs, files in os.walk(args.database):
        for filename in files:
            srcrootp = srclst.get(filename)
            if not srcrootp:
                continue
            srcpath = os.path.join(srcrootp[0], filename)
            dstpath = os.path.join(root, filename)
            print "replace", srcpath, dstpath
            shutil.copy(srcpath, dstpath)