python 把文件夹及子文件夹内的所有文件移动到指定文件夹中
【1】提取路径下所有文件到指定文件夹下
import shutil
import os
def move_files(source, dest):
for root, dirs, files in os.walk(source):
for file in files:
source_path = os.path.join(root, file)
destination_path = os.path.join(dest, file)
shutil.move(source_path, destination_path)
# 请填写要移动的源文件夹和目标文件夹的路径
source_folder = "old"
destination_folder = "new_20230401"
move_files(source_folder, destination_folder)
【2】提取路径下所有指定后缀文件夹到指定文件夹下 ,已txt为例
import shutil
import os
def move_txt_files(source, dest):
for root, dirs, files in os.walk(source):
for file in files:
if file.endswith('.txt'):
source_path = os.path.join(root, file)
destination_path = os.path.join(dest, file)
shutil.move(source_path, destination_path)
# 请填写要移动的源文件夹和目标文件夹的路径
source_folder = "src_folder"
destination_folder = "dst_folder"
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
move_txt_files(source_folder, destination_folder)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)