Python 批量将 TXT 文件中的图片路径复制到文件夹
你可以使用 Python 的 shutil 模块来完成这个任务。下面是一段完整的 Python 代码,可以读取 txt 文件中的图片路径,并将这些图片复制到指定的文件夹中:
import os
import shutil
# 指定 txt 文件路径和要保存图片的文件夹路径
txt_file = 'path/to/your/txt/file.txt'
output_folder = 'path/to/your/output/folder/'
# 创建保存图片的文件夹
os.makedirs(output_folder, exist_ok=True)
# 读取 txt 文件中的图片路径
with open(txt_file, 'r') as file:
lines = file.readlines()
# 复制图片到指定的文件夹中
for line in lines:
# 去除换行符
line = line.strip()
# 获取图片文件名
img_name = os.path.basename(line)
# 拼接输出路径
output_path = os.path.join(output_folder, img_name)
# 复制图片文件
shutil.copy2(line, output_path)
print('图片复制完毕!')
你只需将代码中的 txt_file 和 output_folder 变量替换为你的实际路径,然后运行代码,即可将 txt 文件中的图片复制到指定的文件夹中。
原文地址: https://www.cveoy.top/t/topic/phVU 著作权归作者所有。请勿转载和采集!