我是靠谱客的博主 贪玩天空,这篇文章主要介绍python将多个文件夹下的内容合并到一个文件夹下,现在分享给大家,希望可以做个参考。

# 原文件夹
old_path = "G:/MP4"
# 查看原文件夹下所有的子文件夹
filenames = os.listdir(old_path)
# 新文件夹
target_path = "G:/MP5"
if not os.path.exists(target_path):
    os.mkdir(target_path)

for file in filenames:
    # 所有的子文件夹
    sonDir = "G:/MP4/" + file
    # 遍历子文件夹中所有的文件
    for root, dirs, files in os.walk(sonDir):
    	# 如果文件夹中有文件
        if len(files) > 0:
            for f in files:
                newDir = sonDir + '/' + f
                # 将文件移动到新文件夹中
                shutil.move(newDir, target_path)
        else:
            print(sonDir + "文件夹是空的")

有个更简单的方法https://blog.csdn.net/qq_42335165/article/details/86218786

最后

以上就是贪玩天空最近收集整理的关于python将多个文件夹下的内容合并到一个文件夹下的全部内容,更多相关python将多个文件夹下内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部