python系統模組

來源:互聯網
上載者:User

標籤:tom   replace   sage   http   types   loaded   input   handle   oat   

Python中大多數系統介面都集中在兩個模組:sys和os.這麼說有點過於簡單化 還有一些其他的錶轉模組也屬於這個領域他們包括:

glob 用於檔案名稱的擴充

socket 用於網路連接和處理序間通訊(IPC)

threading,_threading,queue 用於運行和同步話並發線程

time,timeit 用於擷取系統時間相關細節。

subprocess,multiprocessing 用於啟動和控制並行進程

signal,select,shutil,tempfile 用於多種系統相關任務

還有諸如pySerial(一種序列埠介面),Pexpect(一個用於控製程序間對話的類Expect系統) 甚至TWisted(一種網路架構) 等等第三方擴充包也歸入系統編程領域。另外 一些內建函數實際上也是系統介面 例如open函數 就是檔案系統介面 但大體而言sys和os模組一起組成了pYthon內建系統工具的核心部分

從原則上講 sys負責匯出與python解譯器本身相關的組件(如模組搜尋路徑) os則包含於python所在底層作業系統相對應的變數和函數 實踐中這種區別並不總是這麼明顯(如sys包含了標準輸入輸出資料流) 但也可以說它們與作業系統模式有關

擷取模組文檔

學慣用python來編寫系統在很大程度上就是學習python的系統模組

例如我們要知道一個內建模組匯出的所有東西 可以在庫手冊裡閱讀相關條目 也可以閱讀它的源碼 或者互動擷取它的屬性列表和文檔字串 

我們匯入ptyhon3.1sys模組 看一看它為我們提供了什麼好東西

Microsoft Windows [版本 6.1.7601]
著作權 (c) 2009 Microsoft Corporation。著作權所有,並保留一切權利。

C:\Users\Administrator.WIN-20160322KSZ>
C:\Users\Administrator.WIN-20160322KSZ>D:

D:\>dir
磁碟機 D 中的卷是 軟體
卷的序號是 000F-9CF8

D:\ 的目錄

2016/07/03 21:43 6,862,200 1.zip
2016/09/14 11:17 1,325,052 code_short.pcm
2016/08/21 13:20 24 config.ini
2016/06/19 01:52 2,558 day6.py
2016/10/16 19:43 <DIR> Documents
2016/11/22 01:02 <DIR> Downloads
2016/03/21 19:45 <DIR> Favorites
2016/03/27 14:45 <DIR> kuwo
2016/11/28 23:09 <DIR> Mozilla Firefox
2016/06/21 10:52 <DIR> pdf
2016/05/15 19:16 <DIR> PP助手2.0
2016/05/02 23:02 <DIR> Program Files
2004/07/03 09:48 139,264 psicon.dll
2004/10/24 09:19 1,112 psicon.reg
2016/06/25 22:31 198,296 pyexpat.pyd
2016/05/22 20:53 <DIR> Python
2016/06/25 22:31 42,136 python.exe
2016/06/25 22:31 52,376 python3.dll
2016/06/25 22:31 3,931,288 python35.dll
2016/06/25 22:31 2,257,578 python35.zip
2016/06/25 22:31 42,136 pythonw.exe
2016/06/25 22:31 17 pyvenv.cfg
2016/10/06 13:07 <DIR> QQVipDownload
2016/11/22 15:10 <DIR> qycache
2016/06/17 15:32 <DIR> Screenshots
2016/06/25 22:31 26,264 select.pyd
2016/06/25 22:31 773,784 sqlite3.dll
2016/08/21 19:11 <DIR> Thunder Network
2016/06/25 22:31 871,576 unicodedata.pyd
2016/06/25 22:31 89,416 vcruntime140.dll
2016/02/12 13:16 <DIR> win7dock
2016/06/25 22:31 25,752 winsound.pyd
2016/06/17 22:45 <DIR> XY蘋果助手
2016/06/25 22:31 91,288 _bz2.pyd
2016/06/25 22:31 129,176 _ctypes.pyd
2016/06/25 22:31 321,176 _decimal.pyd
2016/06/25 22:31 199,832 _elementtree.pyd
2016/06/25 22:31 1,444,504 _hashlib.pyd
2016/06/25 22:31 152,728 _lzma.pyd
2016/06/25 22:31 38,552 _msi.pyd
2016/06/25 22:31 28,824 _multiprocessing.pyd
2016/06/25 22:31 41,112 _overlapped.pyd
2016/06/25 22:31 72,344 _socket.pyd
2016/06/25 22:31 78,488 _sqlite3.pyd
2016/06/25 22:31 2,044,056 _ssl.pyd
2016/09/11 22:38 <DIR> 常用軟體
2016/03/22 20:31 <DIR> 英雄聯盟
2016/03/22 22:15 466 軟體 (D).lnk
31 個檔案 21,283,375 位元組
17 個目錄 113,867,571,200 可用位元組

D:\>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys)
[‘__displayhook__‘, ‘__doc__‘, ‘__excepthook__‘, ‘__interactivehook__‘, ‘__loade
r__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘__stderr__‘, ‘__stdin__‘, ‘__stdou
t__‘, ‘_clear_type_cache‘, ‘_current_frames‘, ‘_debugmallocstats‘, ‘_getframe‘,
‘_home‘, ‘_mercurial‘, ‘_xoptions‘, ‘api_version‘, ‘argv‘, ‘base_exec_prefix‘, ‘
base_prefix‘, ‘builtin_module_names‘, ‘byteorder‘, ‘call_tracing‘, ‘callstats‘,
‘copyright‘, ‘displayhook‘, ‘dllhandle‘, ‘dont_write_bytecode‘, ‘exc_info‘, ‘exc
epthook‘, ‘exec_prefix‘, ‘executable‘, ‘exit‘, ‘flags‘, ‘float_info‘, ‘float_rep
r_style‘, ‘get_coroutine_wrapper‘, ‘getallocatedblocks‘, ‘getcheckinterval‘, ‘ge
tdefaultencoding‘, ‘getfilesystemencoding‘, ‘getprofile‘, ‘getrecursionlimit‘, ‘
getrefcount‘, ‘getsizeof‘, ‘getswitchinterval‘, ‘gettrace‘, ‘getwindowsversion‘,
‘hash_info‘, ‘hexversion‘, ‘implementation‘, ‘int_info‘, ‘intern‘, ‘is_finalizi
ng‘, ‘maxsize‘, ‘maxunicode‘, ‘meta_path‘, ‘modules‘, ‘path‘, ‘path_hooks‘, ‘pat
h_importer_cache‘, ‘platform‘, ‘prefix‘, ‘ps1‘, ‘ps2‘, ‘set_coroutine_wrapper‘,
‘setcheckinterval‘, ‘setprofile‘, ‘setrecursionlimit‘, ‘setswitchinterval‘, ‘set
trace‘, ‘stderr‘, ‘stdin‘, ‘stdout‘, ‘thread_info‘, ‘version‘, ‘version_info‘, ‘
warnoptions‘, ‘winver‘]
>>>

dir函數會簡單返回一個列表 其中包含了帶屬性隊形的所有屬性的字串名稱 這是一種在互動提示符下喚醒對模組的記憶便捷方式 

例如dir函數結果中有version的存在 我們得一知道有一個sys.version  

>>>sys.__doc__  內建模組的__doc__字元傳:傳回值如下

"This module provides access to some objects used or maintained by the\ninterpre
ter and to functions that interact strongly with the interpreter.\n\nDynamic obj
ects:\n\nargv -- command line arguments; argv[0] is the script pathname if known
\npath -- module search path; path[0] is the script directory, else ‘‘\nmodules
-- dictionary of loaded modules\n\ndisplayhook -- called to show results in an i
nteractive session\nexcepthook -- called to handle any uncaught exception other
than SystemExit\n To customize printing in an interactive session or to install
a custom\n top-level exception handler, assign other functions to replace thes
e.\n\nstdin -- standard input file object; used by input()\nstdout -- standard o
utput file object; used by print()\nstderr -- standard error object; used for er
ror messages\n By assigning other file objects (or objects that behave like fil
es)\n to these, it is possible to redirect all of the interpreter‘s I/O.\n\nlas
t_type -- type of last uncaught exception\nlast_value -- value of last uncaught
exception\nlast_traceback -- traceback of last uncaught exception\n These three
are only available in an interactive session after a\n traceback has been prin
ted.\n\nStatic objects:\n\nbuiltin_module_names -- tuple of module names built i
nto this interpreter\ncopyright -- copyright notice pertaining to this interpret
er\nexec_prefix -- prefix used to find the machine-specific Python library\nexec
utable -- absolute path of the executable binary of the Python interpreter\nfloa
t_info -- a struct sequence with information about the float implementation.\nfl
oat_repr_style -- string indicating the style of repr() output for floats\nhash_
info -- a struct sequence with information about the hash algorithm.\nhexversion
-- version information encoded as a single integer\nimplementation -- Python im
plementation information.\nint_info -- a struct sequence with information about
the int implementation.\nmaxsize -- the largest supported length of containers.\
nmaxunicode -- the value of the largest Unicode code point\nplatform -- platform
identifier\nprefix -- prefix used to find the Python library\nthread_info -- a
struct sequence with information about the thread implementation.\nversion -- th
e version of this interpreter as a string\nversion_info -- version information a
s a named tuple\ndllhandle -- [Windows only] integer handle of the Python DLL\nw
inver -- [Windows only] version number of the Python DLL\n__stdin__ -- the origi
nal stdin; don‘t touch!\n__stdout__ -- the original stdout; don‘t touch!\n__stde
rr__ -- the original stderr; don‘t touch!\n__displayhook__ -- the original displ
ayhook; don‘t touch!\n__excepthook__ -- the original excepthook; don‘t touch!\n\
nFunctions:\n\ndisplayhook() -- print an object to the screen, and save it in bu
iltins._\nexcepthook() -- print an exception and its traceback to sys.stderr\nex
c_info() -- return thread-safe information about the current exception\nexit() -
- exit the interpreter by raising SystemExit\ngetdlopenflags() -- returns flags
to be used for dlopen() calls\ngetprofile() -- get the global profiling function
\ngetrefcount() -- return the reference count for an object (plus one :-)\ngetre
cursionlimit() -- return the max recursion depth for the interpreter\ngetsizeof(
) -- return the size of an object in bytes\ngettrace() -- get the global debug t
racing function\nsetcheckinterval() -- control how often the interpreter checks
for events\nsetdlopenflags() -- set the flags to be used for dlopen() calls\nset
profile() -- set the global profiling function\nsetrecursionlimit() -- set the m
ax recursion depth for the interpreter\nsettrace() -- set the global debug traci
ng function\n"
>>>

>>>pirnt(sys.__doc__)

該模組提供了對使用或維護的一些對象的訪問
解譯器和與解譯器互動的功能。

print()內建函數會正確地解釋分行符號 遺憾的是本身並不完成頁面滾動或分頁顯示 因此在某些平台上仍會出現令人不滿意的顯示效果 其他工具如內建help(函數) 則做的很好

>>> help(sys)
Help on built-in module sys:

NAME
sys

MODULE REFERENCE
https://docs.python.org/3.5/library\sys.html

The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the
location listed above.

DESCRIPTION
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

Dynamic objects:

argv -- command line arguments; argv[0] is the script pathname if known
path -- module search path; path[0] is the script directory, else ‘‘
modules -- dictionary of loaded modules

-- More --

help函數是PyDoc系統提供的介面之一 PyDoc系統是python內建的標準庫代碼 可將對象相關的文檔 呈現為格式化後的形式 這種格式化可以是類似Unix裡的手冊頁 便於協助使用 也是翻閱相關書籍和手冊之前的最後一招。

 

python系統模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.