简体   繁体   中英

Optimizing simple Python file matching & replacement script

just wondering if anyone has a suggestion for how I can optimize my simple but slow file replacement script:

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)

Thanks!

This is a clean up code:

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)

if args.folder and args.database are separate (not subdir), and all file's name in their dir are unique, then you can do this:

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])

I think this one will be faster if args.folder has just few files.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM