代码拉取完成,页面将自动刷新
import json
import sys
import os
import shutil
def read_config(config_file: str):
if not os.path.exists(config_file):
print("[error]not find config file.")
return
with open(config_file, encoding="utf-8") as config:
return json.loads(config.read())
def check_config(config: dict):
try:
if config is None:
return False
if not os.path.exists(config["input_dir"]):
print("[error]input_dir not exists.")
return False
if not os.path.exists(config["output_dir"]):
print("[error]output_dir not exists.")
return False
dir_maps = config["dir_maps"]
if not isinstance(dir_maps, dict):
print("[error]dir_maps not a json object.")
return False
input_dir = config["input_dir"]
for key, _ in dir_maps.items():
if not os.path.exists(input_dir + "/" + key):
print("[error]", key, "not exists.")
return False
except KeyError:
print("[error]config file format error.")
return False
return True
def check_or_create_dir(parent_dir: str, child_dir: str):
if not os.path.exists(parent_dir + "/" + child_dir):
print("create dir:", child_dir)
os.mkdir(parent_dir + "/" + child_dir)
def copy_all_files(input_dir: str, output_dir: str):
count = 0;
for f in os.scandir(input_dir):
if os.path.isfile(f):
print_copy_message(f.name, output_dir)
shutil.copy(f, output_dir)
count += 1
print(" count:", count)
def file_exists(parent_dir: str, file_name: str):
return os.path.exists(parent_dir + "/" + file_name)
def print_copy_message(file_name: str, output_dir: str):
message = " copy: " + file_name
if file_exists(output_dir, file_name):
message += " [override]"
else:
message += " [new]"
print(message)
def main():
config_file = "copy_config.json"
if len(sys.argv) > 1:
config_file = sys.argv[1]
config = read_config(config_file)
if not check_config(config):
return
input_dir = config["input_dir"]
output_dir = config["output_dir"]
dir_maps = config["dir_maps"]
print("Input Dir:", input_dir)
print("Output Dir:", output_dir)
result = input("\nContinue?(Y/N):")
if result.lower() == "y" :
for key, value in dir_maps.items():
check_or_create_dir(output_dir, value)
print("[", key, " -> ", value, "]", sep="")
copy_all_files(input_dir + "/" + key, output_dir + "/" + value)
input("\nPress any key to exit.")
if __name__ == "__main__":
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。