os模块

本文深入解析Python的os模块,涵盖os.getcwd()获取当前工作目录,os.path.join()连接路径组件,os.path.split()拆分路径,os.walk()遍历目录树,以及str.endswith()检查字符串结尾。这些函数在文件系统操作中极为关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.os.getcwd()

Return an unicode string representing the current working directory.

>>> os.getcwd()
'/home/hushch'

2.os.path.join(a, *p)

Join two or more path components, inserting '/' as needed. If any component is an absolute path. all previous path will discarded.

>>> os.path.join('/a/b', '/c/d') 
'/c/d'
>>> os.path.join('/a/b', 'c/d')
'/a/b/c/d'

'/c/d' 路径是完整的,故前面的'/a/b'就不要了,而'c/d'是不完整的,所以前面的路径保留了。

'/a/b/c/d'
>>> '/a/b'+'c/d'
'/a/bc/d'
>>> '/a/b'+'/c/d'
'/a/b/c/d'

'/a/b' + '/c/d' == os.path.join('/a/b', 'c/d')

3.os.path.split(p)

split a pathname, return a tuple "(head, tail)", where tail is the everything after the final slash. 如:

>>> os.path.split('/a/b/c/d/e/f.jpg')
	    
('/a/b/c/d/e', 'f.jpg')

4. os.walk(top, topdown=True, οnerrοr=None, followlinks=False)

Directory tree generator.

>>> for r, d, f in os.walk(top='/home/hushch/user/Final Project', topdown=True, onerror=None, followlinks=False):
	print("root: %s, dirnames: %s, filenames: %s." % (r, d, f))

root: /home/hushch/user/Final Project, dirnames: ['znyp_format-master', '.idea'], filenames: ['znyp_format-master.tar.gz', 'loaddata.py'].
root: /home/hushch/user/Final Project/znyp_format-master, dirnames: ['znyp_dataset', '.ipynb_checkpoints', 'classes', 'test', 'model', '__pycache__'], filenames: ['convert_images_to_znyp_format.py', 'load_znyp_format.py', 'Untitled21.ipynb', 'README.md', 'Untitled.ipynb', 'resize_image.py'].
root: /home/hushch/user/Final Project/znyp_format-master/znyp_dataset, dirnames: [], filenames: ['test_data', 'placeholder.txt', 'train_data'].
root: /home/hushch/user/Final Project/znyp_format-master/.ipynb_checkpoints, dirnames: [], filenames: ['Untitled21-checkpoint.ipynb', 'Untitled-checkpoint.ipynb'].
root: /home/hushch/user/Final Project/znyp_format-master/classes, dirnames: ['1', '0'], filenames: [].
root: /home/hushch/user/Final Project/znyp_format-master/classes/1, dirnames: [], filenames: ['01.jpg', '02.jpg', '03.jpg', '04.jpg', '05.jpg', '06.jpg', '07.jpg'].
root: /home/hushch/user/Final Project/znyp_format-master/classes/0, dirnames: [], filenames: ['01.jpg', '02.jpg', '03.jpg', '04.jpg', '05.jpg', '06.jpg', '07.jpg'].
root: /home/hushch/user/Final Project/znyp_format-master/test, dirnames: [], filenames: ['01.jpg', 'load_znyp_format.py', '02.jpg'].
root: /home/hushch/user/Final Project/znyp_format-master/model, dirnames: [], filenames: ['Preprocess.py'].
root: /home/hushch/user/Final Project/znyp_format-master/__pycache__, dirnames: [], filenames: ['convert_images_to_znyp_format.cpython-36.pyc'].
root: /home/hushch/user/Final Project/.idea, dirnames: ['dictionaries'], filenames: ['misc.xml', 'workspace.xml', 'Final Project.iml', 'modules.xml'].
root: /home/hushch/user/Final Project/.idea/dictionaries, dirnames: [], filenames: ['hushch.xml'].

root: 当前的节点

dirnames: 节点下的子节点

filenames: 叶子节点

所谓节点是文件夹,叶子节点是文件。 os.walk会遍历底下所有节点。

5. str.endswith(suffix[, start[, end]]) --> bool

判断是不是在[start, end]内以suffix结尾,start, end默认为str的开头和结尾。返回True 或者 False.

start, end 对应字母的位置。

>>> a='I am evil'
>>> a.endswith('evil')
True

6. os.sep

默认分隔符是'/'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值