当前位置:网站首页>学习笔记--文件夹处理--代码学习
学习笔记--文件夹处理--代码学习
2022-08-09 04:43:00 【free_luojing】
场景一: 有一个存放图片的文件夹,读取所有同类型图片,并把图片名字读进一个数组里。
文件夹image下存放着3张同为.jpeg扩展名的图片
img_ids = glob(os.path.join( 'image', '*' + "jpeg"))
print("img_ids",img_ids)
输出结果
['image\\1fregfsf2.jpeg','image\\1f2334rf2.jpeg','image\\1hfudsgfe.jpeg']
img_ids = [os.path.splitext(os.path.basename(p))[0] for p in img_ids]
这句代码等价
img_name=[]
for p in img_ids:
os.path.splitext(os.path.basename(p))[0]
img_name.append(f)
输出结果
['1fregfsf2', '1f2334rf2', '1hfudsgfe']
代码拆分解析
img_name=[]
for p in img_ids:
print("p",p)#逐个输出数组内容
# 输出image\\1fregfsf2.jpeg
i=os.path.basename(p) #返回最后一段文件名,
print("i",i)
# 输出1fregfsf2.jpeg
q=os.path.splitext(i)#分离文件名和扩展名
print("q",q)
# 输出('1fregfsf2','.jpeg')
g=g[0]
# 输出1fregfsf2
img_name.append(f)#读进数组中
总结
img_ids = glob(os.path.join( 'image', '*' + "jpeg"))
img_ids = [os.path.splitext(os.path.basename(p))[0] for p in img_ids]
补充
字符串分离方法
a='crop//image//12kiwofjw99j.jpg'
b=a.split('//',2)[2]
print("b",b)
# 输出 12kiwofjw99j.jpg
img_id = b.split(".", 1)[0]
print("img_id",img_id)
# 输出 12kiwofjw99j
split函数 b=a.split(‘//’,2) b=[‘crop’, ‘image’, ‘12kiwofjw99j.jpg’]
以“//”符号给字符串a进行分离,2分离两次,也就是切成三段。[2]是取数组中的第三个数,这里就是’12kiwofjw99j.jpg’
文件夹内容读取,文件里面套着文件
边栏推荐
- A case of missing heritability
- 阿里云天池大赛赛题(机器学习)——天猫用户重复购买预测(完整代码)
- 【暑期每日一题】洛谷 P5729 【深基5.例7】工艺品制作
- "IP" command to configure network interface
- Alibaba Cloud Tianchi Contest Question (Machine Learning) - Repeat Purchase Prediction of Tmall Users (Complete Code)
- HP路由器和交换机日志分析
- [Server data recovery] A case of data recovery when the Ext4 file system cannot be mounted and an error is reported after fsck
- simple math formula calculation
- 稳定性测试怎么做,这篇文章彻底讲透了!
- FFmpeg编译支持x264/openH264/dash
猜你喜欢
随机推荐
人类微生物组和缺失遗传力--读论文
助力To B业务,这类企业端数据值得风控童鞋关注
数量遗传学遗传力计算2:半同胞和全同胞
Example of 360 assessment feedback questions
XJTUSE Professional Course and Experiment Guide
ABP 6.0.0-rc.1的新特性
Flask框架实现异步处理请求
杰理之SD卡切回蓝牙没有作用【篇】
高效回顾深度学习DL、CV、NLP
Gopacket source code analysis
MySQL: redo log log - notes for personal use
杰理之开关降噪语音识别没有用【篇】
【暑期每日一题】洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here
npm package.json
Divisible property 1
Introduction to JVM garbage collection mechanism
串扰与防护
【暑期每日一题】洛谷 P4325 [COCI2006-2007#1] Modulo
simple math formula calculation
「竞品分析报告」不会写?不知从哪收集数据?请收下这篇竞品指南