python - 如何使用 python 将某些文件从多个子目录复制到一个目标?
问题描述
我是 python 新手,我正在尝试编写一个脚本,将 .txt 文件从不同的子目录移动到一个新文件夹。
import os
import shutil
source_directory = os.path.join('mysourcedirectory', 'source')
target_directory = os.path.join('destinationdirectory', 'target')
operation= 'copy'
for src_dir, dirs, files in os.walk(source_directory):
if file.endswith(".txt"):
shutil.copy(os.path.join(target_directory, file))
有人能告诉我哪里出了问题以及如何修改它吗?我还希望能够修改这些文件(删除标题,将文件重命名为连续数字)。这仍然可以在一个脚本中完成吗?
解决方案
推荐阅读
- javascript - Firebase云功能:从文档创建中执行查询,然后删除除新创建的结果之外的结果
- python - TypeError:迭代 0-d 张量
- spring - SessionCreationPolicy.STATELESS 时显示 spring 安全认证对象
- flutter - 等效于 Riverpod 中的 ChangeNotifierProvider 小部件
- mysql - 字符串部分搜索
- gnuplot - Inkscape 裁剪使用 gnuplot 生成的 eps 文件
- nix - Nix:将其他派生的子目录添加到 PATH
- c - 是否存在可移植的 pthread_sleep() 函数?
- r - 如何执行模拟以在 R 中找到具有给定概率的 z 分数 (x)
- angular - 如何在 Angular/RxJS 中合并两个 observable?