python 常用模块

1.告诉解释器哪里找模块:

>>> import sys
>>> sys.path.append('c:/python')
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__)
D:\AppData\Local\Programs\Python\Python35\lib\copy.py
sys.argv

>>> sys.platform
'win32'


4

>>> 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
双端队列:deque
time. 

 random伪随机。。。os.urandom 真随机。

7

re




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值