Day1_Python基础_9.模块初识
九、模块初识
Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在,我们先来象征性的学2个简单的。
sys
|
1
2
3
4
5
6
7
8
9
10
11
|
#!/usr/bin/env python# -*- coding: utf-8 -*-import sysprint(sys.argv)#输出$ python test.py helo world['test.py', 'helo', 'world'] #把执行脚本时传递的参数获取到了 |
os
|
1
2
3
4
5
6
|
#!/usr/bin/env python# -*- coding: utf-8 -*-import osos.system("df -h") #调用系统命令 |
完全结合一下
|
1
2
3
|
import os,sysos.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行 |
自己写个模块
python tab补全模块
1 #!/usr/bin/env python 2 # python startup file 3 import sys 4 import readline 5 import rlcompleter 6 import atexit 7 import os 8 # tab completion 9 readline.parse_and_bind('tab: complete') 10 # history file 11 histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 12 try: 13 readline.read_history_file(histfile) 14 except IOError: 15 pass 16 atexit.register(readline.write_history_file, histfile) 17 del os, histfile, readline, rlcompleter
写完保存后就可以使用了
|
1
2
3
4
5
|
localhost:~ jieli$ pythonPython 2.7.10 (default, Oct 23 2015, 18:05:06)[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import tab |
你会发现,上面自己写的tab.py模块只能在当前目录下导入,如果想在系统的何何一个地方都使用怎么办呢? 此时你就要把这个tab.py所在的目录放到系统全局环境变量目录里,或者把tab.py放到 ../site-packages 目录下,这个目录在不同的OS里放的位置不一样,用 print(sys.path) 可以查看python环境变量列表。一般第三方库或自己安装的库一般情况下会放在此位置。
师傅说赶路要紧
浙公网安备 33010602011771号