def main(argv): """ :param argv: """ err_msg = 'zip.py -i <input_path> -o <output_path> -n <output_name> [-l <compress_level>]' input_path = '' output_path = '' output_name = '' compress_level = None try: opts, args = getopt.getopt(argv, "hi:o:n:l:", ["input_path=", "output_path=", "output_name=", "compress_level="]) if len(argv) < 6: Console.error(err_msg) sys.exit(2) except getopt.GetoptError as e: Console.error(e.msg, e) sys.exit(2) before = time.time() for opt, arg in opts: if opt == '-h': Console.info(err_msg) sys.exit() elif opt in ("-i", "--input_path"): if arg == "": Console.error("目标路径不能为空!") sys.exit(2) input_path = arg elif opt in ("-o", "--output_path"): if arg == "": Console.error("输出路径不能为空!") sys.exit(2) output_path = arg elif opt in ("-n", "--output_name"): if arg == "": Console.error("输出文件名不能为空!") sys.exit(2) output_name = arg elif opt in ("-l", "--compress_level"): if not arg == "": try: compress_level = int(arg) except ValueError: compress_level = arg r = zip_file_path(input_path, output_path, output_name, compress_level) Console.success("完成压缩,共:", r, "个项目", "\r\n输出文件:", output_path + '/' + output_name, "耗时:", time_escape(time.time() - before))
def get_zip_file(input_path, result): """ 对目录进行深度优先遍历 :param input_path: :param result: :return: """ try: for file in os.listdir(input_path): if os.path.isdir(input_path + '/' + file): result.append(input_path + '/' + file) # 包括空目录 get_zip_file(input_path + '/' + file, result) else: result.append(input_path + '/' + file) except NotADirectoryError as ex: Console.error(ex.filename, ex.strerror) sys.exit(2)
import sys import time from util import time_escape, get_file_as_type, get_encoding, Console if __name__ == "__main__": err_msg = 'min.py -i <input_path> -t <type>' file_list = [] argv = sys.argv[1:] input_path = '' _type = '' debug = True try: opts, args = getopt.getopt(argv, "hi:t:r", ["input_path=", "type="]) if len(argv) < 4: Console.error(err_msg) sys.exit(2) except getopt.GetoptError as e: Console.error(e.msg, e) sys.exit(2) for opt, arg in opts: if opt == '-h': Console.info(err_msg) sys.exit() elif opt in ("-i", "--input_path"): if arg == "": Console.error("目标路径不能为空!") sys.exit(2) input_path = arg elif opt in ("-t", "--type"): if arg == "":