[Python] 纯文本查看 复制代码 import os
import shutil
import fnmatch
import tkinter as tk
from tkinter import filedialog
def choose_directory():
root = tk.Tk()
root.withdraw()
directory = filedialog.askdirectory()
return directory
def copy_folders_with_name(root_dir, target_dir, folder_name_pattern):
if not os.path.exists(target_dir):
os.makedirs(target_dir)
for dirpath, dirnames, filenames in os.walk(root_dir):
for dirname in dirnames:
for folder_name in folder_name_pattern:
if fnmatch.fnmatch(dirname, folder_name):
source_folder_path = os.path.join(dirpath, dirname)
relative_path = os.path.relpath(source_folder_path, root_dir)
target_folder_path = os.path.join(target_dir, relative_path)
if not os.path.exists(os.path.dirname(target_folder_path)):
os.makedirs(os.path.dirname(target_folder_path))
shutil.copytree(source_folder_path, target_folder_path)
selected_directory = choose_directory()
print("源文件路径:", selected_directory)
root_directory = selected_directory
selected_directory = choose_directory()
print("提取保存路径:", selected_directory)
target_directory = selected_directory
folder_name_to_copy = ['DR',"IRI","LBI","RIFile"]
copy_folders_with_name(root_directory, target_directory, folder_name_to_copy)
首先创建你需要保存的新的文件路径,第一次弹窗选择文件夹为你的源文件路径,第二次选择你的创建的新的文件路径,脚本会自动将你所有源文件路径你划线文件名按照源文件目录构成复制到新文件路径。 |