栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

[python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[python

任务:将文件夹中包含 ‘2000’,‘2015’ 的文件复制到指定文件夹

  1. 调用包
import pandas as pd
from tqdm import tqdm
import os,shutil
  1. 指定初始文件夹,目标文件夹
start = r'C:Users...'
destination_00 = r'C:Users...'
destination_15 = r'C:Users...'
  1. 初始文件夹包含两层,第一层是二级文件夹,第二层是待复制的文件
foldlist = os.listdir(start) # 列举start中的文件夹

for fold in tqdm(foldlist): # 遍历foldlist中的每个文件夹
    dep = os.path.join(start, fold) # 获取文件地址
    filelist = os.listdir(dep) # 列举子文件夹下的所有文件名
    # 根据条件筛选指定文件名
    city_2000 = [x for x in filelist if '2000' in x]
    city_2015 = [x for x in filelist if '2015' in x]
	# 指定提取地址
    os.chdir(dep)
    # 提取并复制文件
    for file in city_2000:
        if os.path.isfile(file): # 迭代判断当前路径下所有文件
            shutil.copy(file, os.path.join(destination_00, file)) # 复制到指定地址
    for file in city_2015:
        if os.path.isfile(file):
            shutil.copy(file, os.path.join(destination_15, file))

End


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号