python - 递归地将子目录中的所有文件移动到 Python 中的另一个目录

标签 python directory file-move

标题解释了我所追求的。请注意,子目录不会包含任何目录 文件 (*.JPG)。本质上,只是将文件树中的所有内容上移一层。

例如,来自 ~/someDir/folder1/* , ~/someDir/folder2/* , ... , ~/someDir/folderN/*。我想要将子目录的所有内容带到 ~/someDir/

最佳答案

shutil.move 是移动文件的好选择。

import shutil
import os

source = "/parent/subdir"
destination = "/parent/"
files_list = os.listdir(source)
for files in files_list:
    shutil.move(files, destination)

对于递归移动,您可以尝试 shutil.copytree(SOURCE, DESTINATION)。它只是复制所有文件,如果需要,您可以手动清理源目录。

关于python - 递归地将子目录中的所有文件移动到 Python 中的另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653378/

相关文章:

python - 以系列结尾的 Pandas OR 语句包含

c# - 区分大小写 Directory.Exists/File.Exists

javascript - 如何用一个按钮用 PHP 和 HTML 创建一个目录?

applescript - OS X 10.9 Applescript 更改 : using the `move` command in the "System Events" context to move a file

powershell - 将文件向上移动一个文件夹级别

python - Python 中有一个迭代字典

python - NameError:名称 'process' 未定义(python)

python - Python 的网站如何生成其在线文档?

powershell - Powershell:将文件从源目录递归移动到上一级

Python replace() - 如何避免重复 replace()?