python学习笔记12-模块使用
模块os,sys
什么是模块?
模块os,sys
模块是Python组织代码的一种基本方式
一个Python脚本可以单独运行,也可以导入到另外一个脚本运行,用import hello语句来导入,不用加入.py
什么是Python的 包?
Python的模块可以按照目录组织为包
创建一个包的步骤:
创建一个名字为包名的目录
在改目录下创建一个__init__.py文件
根据需要,在该目录下存放脚本文件或已编译的扩展及子包
import pack.m1,pack.m2,pack.m3
查看Python
sys.path
export PYTHONPAT
In [311]: import sys In [312]: sys.path Out[312]: [‘‘, ‘/usr/local/bin‘, ‘/usr/local/lib/python27.zip‘, ‘/usr/local/lib/python2.7‘, ‘/usr/local/lib/python2.7/plat-linux2‘, ‘/usr/local/lib/python2.7/lib-tk‘, ‘/usr/local/lib/python2.7/lib-old‘, ‘/usr/local/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/site-packages‘, ‘/usr/local/lib/python2.7/site-packages/IPython/extensions‘, ‘/root/.ipython‘] In [313]: import string In [314]: import t --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-314-c2d7a4e4fb09> in <module>() ----> 1 import t ImportError: No module named t#找不到这个模块 [[email protected] ~]# ls /usr/local/lib/python2.7/string.py /usr/local/lib/python2.7/string.py [[email protected] ~]# ls /usr/local/lib/python2.7/os.py /usr/local/lib/python2.7/os.py
自定义python的path,一些自己开发的工具可以放在该目录下
方法一:通过sys.path.append(‘/root/library‘)
[[email protected] ~]# mkdir /root/library In [315]: sys.path.append(‘/root/library‘) In [316]: sys.path Out[316]: [‘‘, ‘/usr/local/bin‘, ‘/usr/local/lib/python27.zip‘, ‘/usr/local/lib/python2.7‘, ‘/usr/local/lib/python2.7/plat-linux2‘, ‘/usr/local/lib/python2.7/lib-tk‘, ‘/usr/local/lib/python2.7/lib-old‘, ‘/usr/local/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/site-packages‘, ‘/usr/local/lib/python2.7/site-packages/IPython/extensions‘, ‘/root/.ipython‘, ‘/root/library‘]
方法二、修改/root/.bashrc
[[email protected] ~]# vim /root/.bashrc
添加一行:export PYTHONPATH=/root/library
[[email protected] ~]# . /root/.bashrc [[email protected] ~]# echo $PYTHONPATH /root/library [[email protected] ~]# ipython In [2]: import sys In [3]: sys.path Out[3]: [‘‘, ‘/usr/local/bin‘, ‘/root/library‘, ‘/usr/local/lib/python27.zip‘, ‘/usr/local/lib/python2.7‘, ‘/usr/local/lib/python2.7/plat-linux2‘, ‘/usr/local/lib/python2.7/lib-tk‘, ‘/usr/local/lib/python2.7/lib-old‘, ‘/usr/local/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/site-packages‘, ‘/usr/local/lib/python2.7/site-packages/IPython/extensions‘,
实例:
In [2]: with open(‘/etc/hosts‘) as fd: ...: s = fd.read() In [4]: s.count(‘\n‘) #统计行数,使用\n换行符作为分割的标准 Out[4]: 19 [[email protected] ~]# vim wordcount.py #!/usr/bin/python def wordCount(s): chars = len(s) #统计字符数 words = len(s.split()) #统计单词数量, lines = s.count(‘\n‘) #统计行数,以\n出现的次数为标准,不能使用lines=len(s.splid(‘\n‘)),这样最后结尾的\n会多统计一行None print lines,words,chars s = open(‘/etc/passwd‘).read() wordCount(s) [[email protected] ~]# python wordcount.py 42 71 2051 [[email protected] ~]# wc /etc/passwd 42 71 2051 /etc/passwd [[email protected] ~]# vim wordcount.py [[email protected] ~]# vim test.py #默认是找的当前目录下的 #!/usr/bin/python import wordcount [[email protected] ~]# python test.py 42 71 2051 [[email protected] ~]# ll #当wordcount.py被当成模块使用的时候,会产生编译文件wordcount.pyc,当再次引用,效率更高 总用量 36 drwxr-xr-x 2 root root 4096 9月 8 04:30 library -rw-r--r-- 1 root root 38 9月 8 04:59 test.py -rw-r--r-- 1 root root 182 9月 8 04:54 wordcount.py -rw-r--r-- 1 root root 424 9月 8 05:00 wordcount.pyc
时间: 2024-12-06 00:55:23