python - 如何将多个文件复制到另一个目录中?
问题描述
我有一个数据集,包括按升序排列的图像,例如images_0001.png
,images_0002.png
...images_0500.png
等等。我想将此图像的特定范围复制到另一个目录中。例如,100 张图像将被复制images_0210.png
到images_0310.png
. 有人知道该怎么做吗?
解决方案
尝试:
import shutil
import os
start_idx = 210
num_of_files = 100
source_path = "source/path/dir"
dest_path = "dest/path/dir"
for idx in range(start_idx, start_idx + num_of_files):
src_file_path = os.path.join(source_path, f"images_{idx:04d}.png")
if os.access(src_file_path, os.R_OK):
shutil.copy(src_file_path, dest_path)
else:
print(f"The file {src_file_path} is not readable")
其中{idx:04d}
是文件索引的 4 个字符表示(例如,如果索引为 12,则为 0012)
推荐阅读
- php - 检查预约空档的可用性
- sql - 我怎样才能最好地在我的表中创建员工和认证记录?
- discord.js - 嘿,如何向我的不和谐机器人添加切换系统?[discord.js]
- flutter - 没有名为“nullOk”的命名参数。上下文!=空?Localizations.localeOf(context, nullOk: true) : null,
- java - 如何使用 SwissEPH 获取日月经度?
- spring-boot - 删除十进制 Java 变量
- javascript - 在 JavaScript 中使用 Promise 和 Async/Await
- python - 在 Python 中,从函数内部更改函数外部对象的操作原理是什么?
- html - 如何在我的 ASP.NET Core MVC 项目中使用 Selected2 搜索栏?
- javascript - 如何根据屏幕大小有条件地运行 JavaScipt?