解压缩,重命名和迁移GDB目录的内容 - Python

时间:2016-02-09 15:47:41

标签: python

目前正在尝试开发一个catch all脚本,当它指向一个文件夹时,它将查看所​​有子文件夹,任何.gdb的内容都将被移动到一个新文件夹。有些GDBS是压缩的,有一个文件夹,里面包含GDB。如果GDB文件在提取后具有相同的名称,它们将附加后缀_2,_3,_4,然后移动到包含其他GDB的文件夹,并且不会与任何文件名冲突。

除重命名外,一切正常。因此它会重命名一个ok,然后开始将文件夹放在另一个gdb中,最终失败。 WindowsError:[错误183]当该文件已存在时无法创建文件。

import shutil, errno, re, os, zipfile, os.path, sys


def unzip(source_filename, dest_dir):
with zipfile.ZipFile(source_filename) as zf:
    for member in zf.infolist():
        words = member.filename.split('/')
        path = dest_dir
        for word in words[:-1]:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir, ''): continue
            path = os.path.join(path, word)
        zf.extract(member, path)


results = []
input_path = sys.argv[1]
output_path = sys.argv[2]


if input_path not in output_path:
    for path, subdirs, files in os.walk(input_path):
            for f in files:                        
                    if f.endswith(".zip") or f.endswith(".ZIP"):
                            unzip(os.path.join(path,f), path)

if input_path not in output_path:
    for path, subdirs, files in os.walk(input_path):
            counter = 2
            for dir_name in subdirs:
                    if os.path.join(path, dir_name).endswith(".gdb") or os.path.join(path, dir_name).endswith(".GDB"):

                            if os.path.exists(output_path + "\\" + dir_name):
                                    print "Moving " + os.path.join(path, dir_name) + " To " + output_path + "\\" + str(counter) + dir_name
                                    os.rename(os.path.join(path, dir_name), output_path + "\\" + dir_name[:-4] + "_" + str(counter) + ".gdb")
                                    counter + 1
                            else:
                                    print "Moving " + os.path.join(path, dir_name) + " To " + output_path
                                    shutil.move(os.path.join(path, dir_name), output_path)
else:

    print "########################################"
    print "########################################"
    print "Please select a different output path"
    print "The output path should not be contained"
    print "         In the input path"
    print "########################################"
    print "########################################"

任何人都可以给予任何帮助将不胜感激。干杯!

0 个答案:

没有答案