10行Python代码助你整理杂乱无章的文件
作者:派森酱 时间:2021-03-08 21:11:12
酱子们大家好,今天继续分享 Python 小工具
今天的小程序,可以一键完成文件整理,一起来看看吧!
按照文件格式整理
具体来说就是不同的文件后缀名称放置到不同的文件夹当中
今天我们只需要使用 os 和 shutil 两个库就足够了。os 是 Python 内置模块,用于操作系统的相关功能,shutil 是用来进行文件移动等操作的。
首先我们先来看几个获取文件后缀的方法
获取文件后缀
方法1:
直接通过 os 库实现
filename = os.path.splitext('1.jpg')
Output:
('1', '.jpg')
得到一个包含文件名和后缀名的元组,此时再想获得文件后缀就很容易了吧~
方法2:
可以直接使用字符串方法 endwith,简单明了
file = "1.jpg".endswith(".jpg")
如果结尾字符是 “.jpg” 的话,那么该方法的返回值则为 True,以此我们可以判断文件的后缀名
方法3:
使用字符串的 split 方法,通过切分来获取后缀名称
file1 = "1.jpg".split('.')
这样可以得到一个列表,第二个元素就是后缀名
接下来我们就可以创建文件夹和拷贝文件了,分别用 os.mkdir 和 shutil.copy 来操作
代码如下:
import os
import shutil
files = os.listdir(".")
for f in files:
if os.path.isfile(f) and not f.startswith('.'):
folder_name = os.path.splitext(f)[-1]
folder_name = folder_name.split(".")[-1]
print(folder_name)
if not os.path.exists(folder_name):
os.mkdir(folder_name)
shutil.copy(f, folder_name)
else:
shutil.copy(f, folder_name)
通过上面的代码,我们就可以在当前目录下顺利地整理文件了
按照文件大小整理
当然有的时候,我们同样希望把大小类似的文件整理到一个文件夹当中,可以通过 os 库的 getsize 函数来获取
print(os.path.getsize("1.jpg"))
这样就可以得到该文件的大小,单位是 KB
完整代码如下:
import os
import shutil
files = os.listdir(".")
for f in files:
if os.path.isfile(f) and not f.startswith('.'):
size = os.path.getsize(f)
if size < 1000:
folder_name = "Under_one_MB"
if not os.path.exists(folder_name):
os.mkdir(folder_name)
shutil.copy(f, folder_name)
else:
shutil.copy(f, folder_name)
elif 10000 <= size < 2000:
folder_name = "Under_two_MB"
if not os.path.exists(folder_name):
os.mkdir(folder_name)
shutil.copy(f, folder_name)
else:
shutil.copy(f, folder_name)
来源:https://mp.weixin.qq.com/s/2i73Zt8ZUztHlii-uLQJfw
标签:Python,整理,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现一次性封装多条sql语句(begin end)
2022-09-27 13:20:34
Dreamweaver MX弹出窗口全攻略
2010-09-05 21:14:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311313962s.jpg)
Django RBAC权限管理设计过程详解
2021-09-02 03:57:07
![](https://img.aspxhome.com/file/2023/3/71353_0s.png)
python time()的实例用法
2022-01-09 02:33:02
值得收藏的10道python 面试题
2022-04-22 12:10:33
![](https://img.aspxhome.com/file/2023/6/84076_0s.png)
python算法学习之桶排序算法实例(分块排序)
2022-09-08 13:11:33
python实现b站直播自动发送弹幕功能
2023-07-13 06:58:15
![](https://img.aspxhome.com/file/2023/8/71298_0s.png)
python用quad、dblquad实现一维二维积分的实例详解
2022-02-17 05:32:51
php5.3 不支持 session_register() 此函数已启用的解决方法
2023-11-16 01:59:39
struts2+jsp+jquery+Jcrop实现图片裁剪并上传实例
2023-06-19 16:30:10
![](https://img.aspxhome.com/file/2023/8/97968_0s.jpg)
Python使用列表和字典实现简单的考试系统详解
2023-11-07 11:14:53
Numpy ndarray 多维数组对象的使用
2023-08-09 07:01:54
![](https://img.aspxhome.com/file/2023/9/63979_0s.png)
Go语言编程中字符串切割方法小结
2023-06-16 01:41:24
Python学习之shell脚本的使用详解
2023-10-01 17:57:51
Django学习之文件上传与下载
2023-09-24 18:42:23
Python如何使用pymongo连接MongoDB数据库并进行相关操作
2023-05-12 01:21:49
![](https://img.aspxhome.com/file/2023/7/72017_0s.png)
序列化Python对象的方法
2022-07-09 22:51:59
ajax取消挂起请求的处理方法
2023-11-20 23:41:53
Python 合并多个TXT文件并统计词频的实现
2023-01-09 12:16:08
![](https://img.aspxhome.com/file/2023/7/92247_0s.png)
使用python进行文本预处理和提取特征的实例
2022-07-13 21:25:47