auther: Lart
date: 2019-01-17
update: 2019-01-18 09:55:36html
針對某些操做, 官方推薦這些操做node
This module provides a portable way of using operating system dependent functionality.python
open()
os.path
modulefileinput
moduletempfile
moduleshutil
module.# 在操做系統中定義的環境變量,所有保存在os.environ這個變量中,能夠直接查看: >>> os.environ environ({...'LD_LIBRARY_PATH': '/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/lib64', ..., 'LC_IDENTIFICATION': 'zh_CN.UTF-8', ...}) # 要獲取某個環境變量的值,能夠調用以下操做: >>> os.environ['MANPATH'] '/home/lart/texlive/2018/texmf-dist/doc/man:/usr/local/man:' >>> os.environ.get('MANPATH') '/home/lart/texlive/2018/texmf-dist/doc/man:/usr/local/man:' >>> os.environ.get('MANPATH', 'not found') '/home/lart/texlive/2018/texmf-dist/doc/man:/usr/local/man:' >>> os.environ.get('MAINPATH', 'not found') 'not found' >>> os.environ('MAINPATH') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '_Environ' object is not callable >>> os.getenv('MANPATH') '/home/lart/texlive/2018/texmf-dist/doc/man:/usr/local/man:' >>> os.getenv('MAINPATH', "not found") 'not found' # os.getenv最大的差別就在於不存在路徑的時候, 不會引起異常 >>> os.getenv('MAINPATH')
python調用Shell腳本,有兩種方法:os.system(command)
或os.popen(command)
, 前者返回值是腳本的退出狀態碼, 後者的返回值是腳本執行過程當中的輸出內容. 實際使用時視需求狀況而選擇.linux
# 經過 os.popen() 返回的是 file read 的對象,對其進行讀取 read() 的操做能夠看到執行的輸出。 output = os.popen('cat /proc/cpuinfo') print(output.read())
import sys
)# 獲取系統類型 >>> os.name 'posix' >>> os.uname() posix.uname_result(sysname='Linux', nodename='lart', release='4.15.0-43-generic', version='#46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018', machine='x86_64') >>> os.uname()[0] 'Linux' >>> os.uname()[4] 'x86_64' >>> import sys >>> sys.platform 'linux' # 獲取當前路徑/父路徑的標識符 >>> os.curdir '.' >>> os.pardir '..' # 獲取路徑分隔符和換行符的表示 >>> os.sep '/' >>> os.linesep '\n' # 獲取當前系統環境變量分隔符 >>> os.pathsep ':' # 獲取cpu核心數 # 此數字不等於當前進程可使用的CPU數量。可使用`len(os.sched_getaffinity(0))`得到可用CPU的數量 >>> os.cpu_count() 12 >>> len(os.sched_getaffinity(0)) 12
OSError
FileExistsError
is raised.OSError
if the leaf directory could not be successfully removedexist_ok
is False (the default), an OSError
is raised if the target directory already exists.makedirs()
will become confused if the path elements to create include pardir (eg. 「..」 on UNIX systems).src
改成dst
OSError
will be raised. 若是src
參數對應文件或目錄不存在, 以及若是dst
參數對應文件或目錄已經存在,都會報錯replace()
.###################################################################################### # 返回當前進程工做路徑 >>> os.getcwd() '/home/lart/md/python總結' ###################################################################################### # 返回指定path下的文件夾與文件(包含後綴名)的完整名字, 不包含'.', '..'這樣的特殊目錄 >>> os.listdir('.') ['converter.py', 'face++.py', 'Face++.ipynb', 'person-young-man-beard-emotions-157966.png', '.ipynb_checkpoints', '.idea', 'supervisely.py', 'facepp-python-sdk-master'] ###################################################################################### # 使用內容管理器管理os.scandir迭代器, 返回的是一個`os.DirEntry`對象, 相比`os.listdir`能夠得到更多的信息 # `os.DirEntry` Object yielded by scandir() to expose the file path and other file attributes of a directory entry. # 包含以下屬性與方法: `name, path, inode(), is_dir(*, follow_symlinks=True), is_file(*, follow_symlinks=True), is_symlink(), stat(*, follow_symlinks=True)` >>> with os.scandir('.') as it: ... for entry in it: ... if not entry.name.startswith('.') and entry.is_file(): ... print(entry.name) ... if entry.is_dir(): ... print(f"{entry.name}是名字") ... converter.py face++.py Face++.ipynb person-young-man-beard-emotions-157966.png .ipynb_checkpoints是名字 .idea是名字 supervisely.py facepp-python-sdk-master是名字
有目錄:git
➜ tool_scripts tree . ├── converter.py ├── Face++.ipynb ├── facepp-python-sdk-master │ ├── call_four_task.ipynb │ ├── call.py │ ├── facepp_custom.py │ ├── imgResource │ │ ├── demo.jpeg │ │ ├── gray_image.png │ │ ├── merge.jpg │ │ ├── resultImg.jpg │ │ ├── resultImg.png │ │ ├── search.png │ │ ├── segment.b64 │ │ └── segment.jpg │ ├── PythonSDK │ │ ├── compat.py │ │ ├── facepp.py │ │ ├── ImagePro.py │ │ ├── __pycache__ │ │ │ ├── compat.cpython-36.pyc │ │ │ ├── facepp.cpython-36.pyc │ │ │ ├── ImagePro.cpython-36.pyc │ │ │ └── structures.cpython-36.pyc │ │ └── structures.py │ ├── Python SDK demo 使用文檔.pdf │ └── README.md ├── face++.py ├── person-young-man-beard-emotions-157966.png └── supervisely.py 4 directories, 26 files
###################################################################################### # os.walk(top, topdown=True, onerror=None, followlinks=False) # top 是要便利的目錄的地址, 爲最上層的地址 # topdown 爲True(默認爲True),則優先遍歷父目錄(先把父目錄裏的全部文件(文件夾)遍歷完, 在搜索子文件夾),不然優先遍歷top的子目錄 (廣度優先與深度優先) # onerror 須要一個callable對象,當walk須要異常時,會調用 # followlinks若是爲True,則會遍歷目錄下的快捷方式(linux下是`symbolic link`)實際所指的目錄(默認False) # # os.walk 的返回值是一個生成器(generator),也就是說不斷的遍歷它,來得到全部的內容。 # 每次遍歷的對象都是返回的是一個三元組`(dirpath, dirnames, filenames)` # dirpath (string)當前正在遍歷的這個文件夾的自己的地址 # dirnames (list)**該文件夾中** 全部的 *子文件夾* 的名字(不包括子目錄, excluding '.' and '..') # filenames (list)**該文件夾中** 全部的 *文件* 的名字 # 注意: # - 名字列表中不包含完整路徑, 要想獲得完整的路徑, 可使用`os.path.join(dirpath, name)`. # - 當設定`followlinks`爲True時, 如果連接指向父文件夾, 會致使無線遞歸, 由於`.walk()`不會關注搜索過得部分 # - 若是傳遞相對路徑名,請不要在`walk()`的恢復之間更改當前工做目. `walk()`從不更改當前目錄, 並假定其調用者也不會 ############################################################################### # 下面使用了`topdown=True` >>> for dirpath, dirnames, filenames in os.walk('.'): ... print(dirpath) ... print(dirnames) ... print(filenames) ... . ['.ipynb_checkpoints', '.idea', 'facepp-python-sdk-master'] ['converter.py', 'face++.py', 'Face++.ipynb', 'person-young-man-beard-emotions-157966.png', 'supervisely.py'] ./.ipynb_checkpoints [] ['Face++-checkpoint.ipynb'] ./.idea [] ['misc.xml', 'modules.xml', 'workspace.xml', 'tool_scripts.iml', 'encodings.xml'] ./facepp-python-sdk-master ['imgResource', 'PythonSDK', '.ipynb_checkpoints', '.idea'] ['call.py', 'facepp_custom.py', 'call_four_task.ipynb', 'Python SDK demo 使用文檔.pdf', 'README.md', '.gitignore'] ./facepp-python-sdk-master/imgResource [] ['demo.jpeg', 'resultImg.jpg', 'merge.jpg', 'gray_image.png', 'segment.jpg', 'search.png', 'segment.b64', 'resultImg.png'] ./facepp-python-sdk-master/PythonSDK ['__pycache__'] ['compat.py', 'facepp.py', 'ImagePro.py', 'structures.py'] ./facepp-python-sdk-master/PythonSDK/__pycache__ [] ['compat.cpython-36.pyc', 'facepp.cpython-36.pyc', 'ImagePro.cpython-36.pyc', 'structures.cpython-36.pyc'] ./facepp-python-sdk-master/.ipynb_checkpoints [] ['call_four_task-checkpoint.ipynb'] ./facepp-python-sdk-master/.idea [] ['misc.xml', 'modules.xml', 'facepp-python-sdk-master.iml', 'workspace.xml', 'encodings.xml'] ############################################################################### # 下面使用了`topdown=False` >>> for dirpath, dirnames, filenames in os.walk('.', topdown=False): ... print(dirpath) ... print(dirnames) ... print(filenames) ... ./.ipynb_checkpoints [] ['Face++-checkpoint.ipynb'] ./.idea [] ['misc.xml', 'modules.xml', 'workspace.xml', 'tool_scripts.iml', 'encodings.xml'] ./facepp-python-sdk-master/imgResource [] ['demo.jpeg', 'resultImg.jpg', 'merge.jpg', 'gray_image.png', 'segment.jpg', 'search.png', 'segment.b64', 'resultImg.png'] ./facepp-python-sdk-master/PythonSDK/__pycache__ [] ['compat.cpython-36.pyc', 'facepp.cpython-36.pyc', 'ImagePro.cpython-36.pyc', 'structures.cpython-36.pyc'] ./facepp-python-sdk-master/PythonSDK ['__pycache__'] ['compat.py', 'facepp.py', 'ImagePro.py', 'structures.py'] ./facepp-python-sdk-master/.ipynb_checkpoints [] ['call_four_task-checkpoint.ipynb'] ./facepp-python-sdk-master/.idea [] ['misc.xml', 'modules.xml', 'facepp-python-sdk-master.iml', 'workspace.xml', 'encodings.xml'] ./facepp-python-sdk-master ['imgResource', 'PythonSDK', '.ipynb_checkpoints', '.idea'] ['call.py', 'facepp_custom.py', 'call_four_task.ipynb', 'Python SDK demo 使用文檔.pdf', 'README.md', '.gitignore'] . ['.ipynb_checkpoints', '.idea', 'facepp-python-sdk-master'] ['converter.py', 'face++.py', 'Face++.ipynb', 'person-young-man-beard-emotions-157966.png', 'supervisely.py']
os.path
>>> os.path.commonprefix(['/usr/lib', '/usr/local/lib']) '/usr/l' >>> os.path.commonpath(['/usr/lib', '/usr/local/lib']) '/usr'
>>> path = '/home/lart/Datasets/tool_scripts' >>> os.path.dirname(path) '/home/lart/Datasets' >>> path = '/home/lart/Datasets/tool_scripts/converter.py' >>> os.path.dirname(path) '/home/lart/Datasets/tool_scripts'
'/home/lart/Datasets' >>> os.path.exists(path) True >>> path = '/home/lart/Datasets/tool_scripts/converter.py' >>> os.path.exists(path) True
~
爲用戶目錄(HOME)實際值>>> path = '~/Datasets/tool_scripts/converter.py' >>> os.path.expanduser(path) '/home/lart/Datasets/tool_scripts/converter.py' >>> path = '~/Datasets/tool_scripts' >>> os.path.expanduser(path) '/home/lart/Datasets/tool_scripts'
>>> path = '/home/lart/Datasets/tool_scripts' >>> os.path.getctime(path) 1547694633.120985 >>> os.path.getctime(path + '/converter.py') 1546766886.501085
>>> path = '/home/lart/Datasets/tool_scripts' >>> os.path.getsize(path) 4096 >>> os.path.getsize(path + '/converter.py') 857
os.path.join("c:", "foo")
represents a path relative to the current directory on drive C: (c:foo)
, not c:\foo
.A//B
,A/B/
,A/./B
和A/foo/../B
都變爲A/B
.normcase()
(head, tail)
,其中tail是最後一個路徑名組件,head是指向該路徑的全部內容, tail永遠不會有斜線; 若是path以斜線結尾,則tail將爲空; 若是路徑中沒有斜槓,則head將爲空path
爲空,則head和tail都爲空join(head, tail)
返回與path相同位置的路徑(但字符串可能不一樣).(drive, tail)
,其中drive是掛載點或空字符串. 在不使用驅動器規格的系統上,drive將始終爲空字符串drive + tail
將與path
相同。(root, ext)
, 使得root + ext == path
,ext爲空或以.
開頭,最多包含一個句點. basename
的前導.
被忽略splitext('.cshrc')
返回('.cshrc','')
。glob模塊根據Unix shell使用的規則查找與指定模式匹配的全部路徑名,儘管結果以任意順序返回。正則表達式
實現了:shell
*
表示匹配0到多個字符?
表示匹配一個字符[]
表示匹配括號裏表示的字符範圍
[exp]
匹配指定範圍內的字符,如:[1-9]
匹配1至9範圍內的字符, 破折號(-)表示一個範圍, 上面的範圍也可直接用[0123456789]來表示[!exp]
匹配不在指定範圍內的字符'[?]'
匹配字符'?'
glob中模式規則不是正則表達式, 而是, 符合標準Uinx路徑擴展規則. 可是Shell變量名和符號(~)是不被擴充的, 只有一些特殊的字符: 兩個不一樣的通配符和字母範圍被支持(見上). 對於~
符和shell變量擴展,請使用os.path.expanduser()
和os.path.expandvars(0)
. 模塊規則適合於文檔名的片斷(以/為分隔, 也就是隻能匹配/
與/
之間的文本), 但模式中的路徑能夠是相對或者絕對路徑.ide
這是經過一致地使用os.scandir()
和fnmatch.fnmatch()
函數來完成的,而不是經過實際調用子shell。請注意,與fnmatch.fnmatch()
不一樣,glob將以點(.)
開頭的文件名視爲特殊狀況, 通配符不會進行匹配. 可見後面的例子.函數
sorted()
排序)匹配路徑名字列表, 這個pathname
必須是包含路徑規範的字符串recursive
爲true
,則模式**
將匹配任何文件以及零個或多個目錄和子目錄, 若是模式後跟os.sep
,則只有目錄和子目錄匹配**
模式可能會消耗過多的時間。>>> import glob >>> path '/home/lart/Datasets/tool_scripts' >>> glob.iglob(path + '/*') <generator object _iglob at 0x7fdca3773930> # 這裏只返回下一級 >>> glob.glob(path + '/*', recursive=True) ['/home/lart/Datasets/tool_scripts/converter.py', '/home/lart/Datasets/tool_scripts/face++.py', '/home/lart/Datasets/tool_scripts/Face++.ipynb', '/home/lart/Datasets/tool_scripts/person-young-man-beard-emotions-157966.png', '/home/lart/Datasets/tool_scripts/supervisely.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master'] # 這裏會遍歷全部的子目錄和文件 >>> glob.glob(path + '/**', recursive=True) ['/home/lart/Datasets/tool_scripts/', '/home/lart/Datasets/tool_scripts/converter.py', '/home/lart/Datasets/tool_scripts/face++.py', '/home/lart/Datasets/tool_scripts/Face++.ipynb', '/home/lart/Datasets/tool_scripts/person-young-man-beard-emotions-157966.png', '/home/lart/Datasets/tool_scripts/supervisely.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/call.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/demo.jpeg', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/resultImg.jpg', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/merge.jpg', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/gray_image.png', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/segment.jpg', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/search.png', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/segment.b64', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/imgResource/resultImg.png', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/compat.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/facepp.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/__pycache__', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/__pycache__/compat.cpython-36.pyc', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/__pycache__/facepp.cpython-36.pyc', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/__pycache__/ImagePro.cpython-36.pyc', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/__pycache__/structures.cpython-36.pyc', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/ImagePro.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/PythonSDK/structures.py', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/facepp_custom.py','/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/call_four_task.ipynb', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/Python SDK demo 使用文檔.pdf', '/home/lart/Datasets/tool_scripts/facepp-python-sdk-master/README.md'] >>> glob.glob('./[0-9].*') ['./1.gif', './2.txt'] >>> glob.glob('*.gif') ['1.gif', 'card.gif'] >>> glob.glob('?.gif') ['1.gif'] >>> glob.glob('**/*.txt', recursive=True) ['2.txt', 'sub/3.txt'] >>> glob.glob('./**/', recursive=True) ['./', './sub/']
If the directory contains files starting with .
they won’t be matched by default. 須要在匹配路徑名字上補上符號.
測試
For example, consider a directory containing card.gif
and .card.gif
:
>>> >>> import glob >>> glob.glob('*.gif') ['card.gif'] >>> glob.glob('.c*') ['.card.gif']
escape('//?/c:/Quo vadis?.txt')
將返回'//?/c:/Quo vadis[?].txt'
.[?]
即是字符?
的轉義# https://hk.saowen.com/a/83de58e8a7e060d8ace69c912cbd209948d6c4fe533aa2746201d96a1b45a8bf import glob specials = '?*[' for char in specials: pattern = 'dir/*' + glob.escape(char) + '.txt' print('Searching for: {!r}'.format(pattern)) for name in sorted(glob.glob(pattern)): print(name) print() # 輸出 Searching for: 'dir/*[?].txt' dir/file?.txt Searching for: 'dir/*[*].txt' dir/file*.txt Searching for: 'dir/*[[].txt' dir/file[.txt
# https://www.jianshu.com/p/b1f24d56d73b >>> glob.escape('./**.?.*.[a-z]') './[*][*].[?].[*].[[]a-z]'
此模塊提供對Unix shell樣式通配符的支持,這些通配符與正則表達式(在re模塊中記錄)不一樣。shell樣式通配符中使用的特殊字符是:
*
表示匹配0到多個字符?
表示匹配一個字符[]
表示匹配括號裏表示的字符範圍
[exp]
匹配指定範圍內的字符,如:[1-9]
匹配1至9範圍內的字符, 破折號(-)表示一個範圍, 上面的範圍也可直接用[0123456789]來表示[!exp]
匹配不在指定範圍內的字符'[?]'
匹配字符'?'
請注意:
/
)對於此模塊並非特殊字符。與glob
模塊略有不一樣.
開頭的文件名對於此模塊並非特殊字符,而且可用*
和?
匹配主要的方法:
os.path.normcase()
進行大小寫規範化。fnmatchcase()
可用於執行區分大小寫的比較,不管這是不是操做系統的標準。os.path.normcase()
。[n for n in names if fnmatch(n, pattern)]
相同,但實現更有效。re.match()
一塊兒使用。對於目錄:
>>> print(os.popen('tree -a -L 2').read()) . ├── converter.py ├── Face++.ipynb ├── facepp-python-sdk-master │ ├── call_four_task.ipynb │ ├── call.py │ ├── facepp_custom.py │ ├── .gitignore │ ├── .idea │ ├── imgResource │ ├── .ipynb_checkpoints │ ├── PythonSDK │ ├── Python SDK demo 使用文檔.pdf │ └── README.md ├── face++.py ├── .idea │ ├── encodings.xml │ ├── misc.xml │ ├── modules.xml │ ├── tool_scripts.iml │ └── workspace.xml ├── .ipynb_checkpoints │ └── Face++-checkpoint.ipynb ├── person-young-man-beard-emotions-157966.png ├── supervisely.py └── .test
從下面的測試能夠看出來, 實際上, glob
應該更爲經常使用一些, fnmatch
模塊不會忽略.
開頭的文件(夾), 這有時候會形成沒必要要的麻煩, 因大多數時候, 是不會須要搜索這些文件夾的.
>>> import fnmatch >>> for file in os.listdir('.'): ... if fnmatch.fnmatch(file, '*'): ... print(file) ... converter.py face++.py .test Face++.ipynb person-young-man-beard-emotions-157966.png .ipynb_checkpoints .idea supervisely.py facepp-python-sdk-master >>> glob.glob('./*') ['./converter.py', './face++.py', './Face++.ipynb', './person-young-man-beard-emotions-157966.png', './supervisely.py', './facepp-python-sdk-master']
>>> import fnmatch, re >>> >>> regex = fnmatch.translate('*.txt') >>> regex '(?s:.*\\.txt)\\Z' >>> reobj = re.compile(regex) >>> reobj.match('foobar.txt') <re.Match object; span=(0, 10), match='foobar.txt'>