当前位置:网站首页>将同一文件夹下的大量文件根据设定分至多组
将同一文件夹下的大量文件根据设定分至多组
2022-06-10 17:37:00 【_囧囧_】
将同一文件夹下的大量文件根据设定分至多组
- 需求分析
在recon文件夹下存在大量文件,观察文件名,我们设计将文件按照recon_后的第一个数字进行分组,那么最终产生的文件夹应为0,1,…,11。
- 编写程序
基于上述假象,我们编写了如下程序,将其复制在recon文件夹下运行即可。
实验结果如下所示:#!/usr/bin/env python import subprocess, threading, time, shutil, os, sys import re file_list = os.listdir() file_set = { } pattern = 'recon_(\d*)_' for file in file_list: result = re.match(pattern, file) if result == None: continue if result.group(1) not in file_set.keys(): file_set[result.group(1)] = [] file_set[result.group(1)].append(file) for key in file_set.keys(): os.path.exists(key) or os.mkdir(key) for item in file_set[key]: mv_cmd = ['mv', item, key] subprocess.call(mv_cmd) print(file_set.keys())
- 扩展
如果根据数量进行划分,例如每个文件夹固定数量的文件,我想也是同样的逻辑,首先获取全部文件名,剔除split.py,创建新文件夹,划分文件。
边栏推荐
猜你喜欢
随机推荐
Leetcode 875. Coco, who likes bananas
线性移动棋
IP summary (tcp/ip volumes 1 and 2)
pwnable start
使用IdentityServer出现过SameSite Cookie这个问题吗?
LeetCode 321. Maximum number of splices***
基于业务沉淀组件 => manage-table
Protocol Gen go grpc 'is not an internal or external command, nor is it a runnable program or batch file
Postman-接口测试工具
树、森林和二叉树的关系
2022上半年信息系统项目管理师论文真题
高数_第6章无穷级数__正项级数的性质
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
c语言学习回顾---1 基础知识回顾
The development of flutter in digital life and the landing practice of Tianyi cloud disk
High number_ Chapter 6 infinite series__ Absolute convergence_ Conditional convergence
c语言---14 循环语句for
True thesis of information system project manager in the first half of 2022
高数_第6章无穷级数__绝对收敛_条件收敛
Summary of vim common commands









