【Python之os模块】使用
2024-09-28 17:57:13
主要介绍在平时遇到的os模块的使用方法;
1. os.path
1.1 os.path.sep
# 系统路径分隔符
# =================
# windows下
print(os.path.sep) # \
# ubuntu 16.04
print(os.path.sep) # /
1.2 os.path.join与os.path.sep.join
# 路径拼接
# =================
# windows下
print(os.path.join('dataset', 'images')) # dataset\images
print(os.path.sep.join(['dataset', 'images'])) # dataset\images
# ubuntu 16.04
print(os.path.join('dataset', 'images')) # dataset/images
print(os.path.sep.join(['dataset', 'images'])) # dataset/images
2. os.work
# 使用如下:
# 文件结构如下:
"""
dataset\
chen\
img1.jpg
img2.jpg
zhen\
img3.jpg
img4.jpg
"""
# =================
path = 'dataset/'
for (rootdir, dirNames, filenames) in os.walk(path):
print(rootDir, dirNames, filenames)
dataset/ ['chen', 'zhen'] []
dataset/chen [] ['img1.jpg', 'img2.jpg']
dataset/zhen [] ['img3.jpg', 'img4.jpg']
最新文章
- POJ 2029 Get Many Persimmon Trees
- 通过printf设置Linux终端输出的颜色和显示方式
- ubuntu 快速安装jre
- 采用CSS3设计的登陆界面
- UDPsocket编程
- Android入门视频推荐
- 【转】Dr.com 5.20破解教程
- shell如何将文件上传至ftp
- JavaScript、jQuery获取页面及个元素高度、宽度
- Android EventBus源代码解析 带你深入理解EventBus
- Python 装饰器(Decorator)
- PMP是什么,PMP最难的是哪些内容?
- PHP实现微信模板消息发送给指定用户
- JS(JavaScript)的初了解6(更新中···)
- 编辑技巧之如何跟PDF文档添加贝茨编号
- python学习总结---学习交流群里的问题总结
- 参数优化-API
- MySQL--派生表Condition Pushdown优化
- 网页中flash设置
- keras中TimeDistributed
热门文章
- MySQL 当记录不存在时insert,当记录存在时update
- 2.4 AppDelegate 的 3 个生命周期
- CodeForces - 1017 C. The Phone Number(数学)
- bzoj 5120 无限之环 &; 洛谷 P4003 —— 费用流(多路增广SPFA)
- JavaScript-Tool-富文本:UEditor
- bzoj2118
- python unittest之断言及示例
- linux命令-xz压缩
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che
- ubuntu14.04装完系统更新后桌面挂了