zbparse/flask_app/货物标/find_zbfile.py

40 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-09-09 17:42:39 +08:00
import os
import shutil
def find_and_copy_files(input_folder, output_folder):
# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)
# 定义支持的文件格式
supported_formats = ('.pdf', '.doc', '.docx')
# 遍历输入文件夹
for root, dirs, files in os.walk(input_folder):
for file in files:
# 检查文件名是否包含“招标”或“竞争性”并且文件格式正确
2024-09-13 15:03:55 +08:00
if ('响应' not in file and '投标' not in file) and \
('竞争性' in file or '招标文件' in file or '磋商' in file) and \
file.endswith(supported_formats):
2024-09-09 17:42:39 +08:00
# 构造完整的文件路径
file_path = os.path.join(root, file)
# 构造输出路径
output_path = os.path.join(output_folder, file)
# 检查输出路径是否存在同名文件
unique_path = output_path
count = 1
while os.path.exists(unique_path):
# 文件名前加上编号格式如filename(1).ext
base, extension = os.path.splitext(output_path)
unique_path = f"{base}({count}){extension}"
count += 1
# 复制文件到唯一的路径
shutil.copy2(file_path, unique_path)
print(f"Copied '{file}' to '{unique_path}'.")
# 使用示例
input_folder = 'Z:\\货物类tb\\投标项目资料' # 输入文件夹路径
output_folder = 'C:\\Users\\Administrator\\Desktop\\货物标\\zbfiles' # 输出文件夹路径
find_and_copy_files(input_folder, output_folder)