1.告訴直譯器**找模組:
>>> import sys
unix要絕度路徑
只有第一次匯入執行。
>>> __name__'__main__'
2. 當做包,必須包含乙個命名為__init__py的檔案(模組)
3.dir看模組裡有什麼
下劃線開始,不是給模組外部用的。過濾:
>>> import copy
>>> [n for n in dir(copy) if not n.startswith('_')]
['error', 'pystringmap', 'builtins', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']
>>> copy.__all__
['error', 'copy', 'deepcopy']
上面是公有介面,如果用:
>>> from copy import *
只能使用其中的函式。要匯入其他的,就要顯式指定
>>> help(copy.copy)
help on function copy in module copy:
copy(x)
shallow copy operation on arbitrary python objects.
see the module's __doc__ string for more info.
>>> print(copy.copy.__doc__)
shallow copy operation on arbitrary python objects.
see the module's __doc__ string for more info.
找原始碼:
>>> print(copy.__file__)
sys.argv
>>> sys.platform'win32'
4
>>> os.sep
'\\'
>>> os.pathsep
';'>>> os.linesep
'\r\n'
>>> os.urandom
>>> os.urandom(6)
b'\x9a\x7fm{g\xfc'
5
啟動另外命令:
os.system('firefox.exe')
os.startfile(r'firefox.exe')
windows啟動外部,仍繼續。unix終止,等待system命令完成。
fileinput
import fileinput # 1 # 1 # 1 # 1
# 2 # 2 # 2 # 2
for line in fileinput.input(inplace=1): # 3 # 3 # 3 # 3
line = line.rstrip() # 4 # 4 # 4 # 4
num = fileinput.lineno() # 5 # 5 # 5 # 5
print('%-40s #%2i' % (line, num)) # 6 # 6 # 6 # 6
# 7 # 7 # 7 # 7
python lino.py lino.py
>>> import heapq
雙端佇列:deque
time.
random偽隨機。。。os.urandom 真隨機。
re
python常用模組
logging 日誌是我們排查問題的關鍵利器,寫好日誌記錄,當我們發生問題時,可以快速定位 範圍進行修改 logging將日誌列印到螢幕,日誌級別大小關係為 critical error warning info debug notset,當然也可以自己定義日誌級別 預設logging預設的日誌級別...
python常用模組
collections提供了幾個便於使用的資料型別。1 namedtuple 這個資料型別生成可以使用呼叫屬性的方法來訪問元素內容的元祖 import collections cc collections.namedtuple sha x y get cc 1,2 print get.x,get.y...
python 常用模組
1 os 包含基本的作業系統功能。2 numpy 科學計算庫,提供了矩陣運算的功能。3 sy 有關python執行環境變數的變數和函式。3 image 影象處理基本庫。5 functools 用於高階函式 指那些作用於函式或者返回其它函式的函式,通常只要是可以被當做函式呼叫的物件就是這個模組的目標。...