# 原文件夹
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