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
默认分隔符是'/'