os模塊

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, onerror=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

默認分隔符是'/'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章