python批量移动文件,每个文件夹内都有一个文件,需要把这些文件提取出来,放到另一个文件夹里,手动太费时间了

移动文件

import os
file_dir = r'C:\Users\98471\AppData\Roaming\kingsoft\office6\templates\download'
file_list = os.listdir(file_dir)
for name in file_list:
    file = os.path.join(file_dir,name)
    file_name = os.listdir(file)
    file = os.path.join(file,file_name[0])
    with open(file,'rb') as f:
        content = f.read()
    dis_name = os.path.join(r'D:\材料\ppt模板',file_name[0])
    print(dis_name)
    with open(dis_name,'wb') as f:  
        f.write(content)
print(file_list)
D:\材料\ppt模板\建筑预算员简历 通用模板.docx
D:\材料\ppt模板\简约商务通用模板.pptx
D:\材料\ppt模板\考勤表通用模板.xlsx
D:\材料\ppt模板\简约PPT通用模板.pptx
D:\材料\ppt模板\应届生简历通用模板.docx
D:\材料\ppt模板\极简清新配色总结汇报.pptx
D:\材料\ppt模板\签到表通用模板.xls
D:\材料\ppt模板\古典风通用模板.ppt
D:\材料\ppt模板\期末成绩单.ett
D:\材料\ppt模板\自我介绍通用模板4.ppt
D:\材料\ppt模板\成绩单.xlsx
D:\材料\ppt模板\改善提案模板.xlsx
D:\材料\ppt模板\简约个人简历自我介绍.pptx
D:\材料\ppt模板\学生阅读计划.xlsx
D:\材料\ppt模板\月计划表(通用版).xlsx
D:\材料\ppt模板\唯美通用模板.pptx
D:\材料\ppt模板\工作计划表.xlsx
D:\材料\ppt模板\学生课程表模板.xlsx
D:\材料\ppt模板\古风梅花通用模板.pptx
D:\材料\ppt模板\小清新自我介绍.pptx
D:\材料\ppt模板\成绩单.xls
D:\材料\ppt模板\灰白色极简风年中工作总结汇报.pptx
D:\材料\ppt模板\一周计划表.xlsx
D:\材料\ppt模板\莫兰迪简约风工作总结汇报.pptx
D:\材料\ppt模板\蓝色简约淡雅教学通用PPT.pptx
D:\材料\ppt模板\简历通用模板.docx
D:\材料\ppt模板\工作计划跟踪表.xlsx
D:\材料\ppt模板\通用模板.pptx
D:\材料\ppt模板\每日计划表.xlsx
D:\材料\ppt模板\极简通用模板.pptx
D:\材料\ppt模板\清新备课教学模板.pptx
D:\材料\ppt模板\教学通用PPT模板.pptx
['0454e35f3039b83759ee2b8a31397b9d', '06f641d4-8bb2-424a-5cb6-95010c728313', '2d4a6ed8-c008-59bd-55c1-fb92546fa202', '30ce362f-34da-8520-ec37-185a88aca56f', '317f26cbbf6c02f871aa7b43865b9655', '3f222276-c146-111c-1e7c-300e755a058d', '407f1d00-300a-f13f-3fb6-d637d6fe25aa', '4e504817-81e7-3e47-d134-363af51cf9dc', '4fe9b0ac71822', '554bf720-6e02-3f84-d476-65347cb03fcd', '5fc86ef9-099a-3e48-e225-b32d7c32205e', '6dbe6d21-c6fa-1a36-15f5-c9a45d623bc3', '749d785f-4bc2-0daa-599f-94fc5ce62f45', '84124f7e-d86e-e4b2-4ccb-f12bc8efd949', '847f24d2-6127-a3fc-6881-64708870dc34', '8fad3750-951a-4f57-c0e9-d86a46e8414e', '910a6f13-479f-8644-dd5e-9296994e3b41', '9e39e148-195c-fd3f-cd68-06bab0ab491f', 'a2cfffae-bd7c-ac48-f1dc-a1ba72b5e2c4', 'a6f08d27-6d29-5712-ae9f-4750316b6830', 'afe28f2e-3089-12ed-6475-ea895210071b', 'b57b0753-1e3f-5ca2-4ccc-75c84bb4055f', 'bd5e9006-93ad-ffc1-ae22-a015e9f64ffa', 'cbb66494-6c01-2ffe-552a-1ec416d7ad50', 'd6419054-f7de-0daf-599f-578d354ae9a0', 'dcd8430ab6187eed279e040d0fe02b12', 'df983700-af11-0fc6-3266-5e037d37f1d1', 'e04b83f1-0b54-17ce-5368-e6eae880738f', 'e5974daf-f52d-f75e-1b2f-141454c64566', 'ecbdf8ca-1799-fa72-7c96-c1969f62bf61', 'ef47e44c-0122-9217-3156-a593adb9410e', 'fbd70d13-122e-2291-a6e6-71ba0889eceb']

效果:
原来:
在这里插入图片描述
运行可以看到32个文件夹内的文件都过来了
在这里插入图片描述


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一个简单的python脚本,用于批量移动文件到另一个硬盘文件夹:import ossourceFolder = r"C:\path\to\source" destFolder = r"C:\path\to\destination"for root, dirs, files in os.walk(sourceFolder): for file in files: src_file = os.path.join(root, file) dst_file = os.path.join(destFolder, file) if not os.path.exists(dst_file): os.rename(src_file, dst_file) ### 回答2: 下面是一个Python编写的批量移动文件到另一个硬盘文件夹的脚本: ```python import os import shutil def move_files(source_dir, destination_dir): # 遍历源文件夹中的所有文件 for filename in os.listdir(source_dir): # 构建源文件的完整路径 source_file = os.path.join(source_dir, filename) # 如果是文件而不是文件夹 if os.path.isfile(source_file): # 构建目标文件的完整路径 destination_file = os.path.join(destination_dir, filename) # 使用shutil库的move()方法移动文件 shutil.move(source_file, destination_file) # 源文件夹路径 source_directory = "C:/source" # 目标文件夹路径 destination_directory = "D:/destination" # 调用move_files函数,将源文件夹中的文件移动到目标文件夹 move_files(source_directory, destination_directory) ``` 以上脚本首先导入了`os`和`shutil`库,`os`库用于操作文件文件夹,`shutil`库用于移动文件。然后定义了一个`move_files`函数,该函数接收源文件夹路径和目标文件夹路径作为参数。在函数中,通过遍历源文件夹中的所有文件,构建源文件的完整路径,并判断是否为文件而不是文件夹。如果是文件,就构建目标文件的完整路径,并使用`shutil.move()`方法将文件移动到目标文件夹中。 最后,定义了源文件夹路径和目标文件夹路径,并调用`move_files`函数来执行文件移动操作。 ### 回答3: 下面是一个批量移动文件到另一个硬盘文件夹Python脚本示例: ```python import os import shutil def move_files(source_directory, destination_directory): # 遍历源文件夹中的文件 for file_name in os.listdir(source_directory): # 构建源文件的完整路径 source_file = os.path.join(source_directory, file_name) # 如果是文件而不是文件夹 if os.path.isfile(source_file): # 构建目标文件夹中的文件路径 destination_file = os.path.join(destination_directory, file_name) # 使用shutil模块的move函数移动文件 shutil.move(source_file, destination_file) print(f"已成功移动文件 {file_name} 到目标文件夹.") # 源文件夹路径 source_directory = 'C:/源文件夹的路径' # 目标文件夹路径 destination_directory = 'D:/目标文件夹的路径' # 执行移动文件操作 move_files(source_directory, destination_directory) ``` 上述脚本中,我们首先导入了Pythonos和shutil模块。然后定义了一个名为`move_files`的函数,该函数接收源文件夹路径和目标文件夹路径作为参数。 在`move_files`函数中,我们使用`os.listdir`遍历源文件夹中的所有文件名。然后构建每个文件的完整路径。接下来,我们检查文件是否为文件而不是文件夹,以确保只移动文件。 在移动文件之前,我们构建目标文件夹文件的完整路径。最后,我们使用`shutil.move`函数将源文件移动到目标文件夹中,并打印成功移动文件名。 最后,我们定义了源文件夹路径和目标文件夹路径,并调用`move_files`函数来执行文件移动操作。 请注意,您需要将源文件夹路径和目标文件夹路径替换为实际的文件夹路径。另外,确保您具有对目标文件夹的写入权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值