1.告诉解释器哪里找模块:
>>> import sys
>>> sys.path.append('c:/python')
unix要绝度路径只有第一次导入执行。
>>> __name__
'__main__'
当做包,必须包含一个命名为__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__)
D:\AppData\Local\Programs\Python\Python35\lib\copy.py
sys.argv
>>> sys.platform
'win32'
>>> os.sep
'\\'
>>> os.pathsep
';'
>>> os.linesep
'\r\n'
>>> os.urandom
<built-in function urandom>
>>> os.urandom(6)
b'\x9a\x7fm{G\xfc'
5
启动另外命令:
os.system('firefox.exe')
os.startfile(r'firefox.exe')
windows启动外部,仍继续。unix终止,等待system命令完成。
6
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
双端队列:dequetime.
random伪随机。。。os.urandom 真随机。
7
re