zoukankan      html  css  js  c++  java
  • 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 sys
     
    print(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 os
     
    os.system("df -h"#调用系统命令

    完全结合一下  

    1
    2
    3
    import os,sys
     
    os.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$ python
    Python 2.7.10 (default, Oct 23 201518:05:06)
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
    Type "help""copyright""credits" or "license" for more information.
    >>> import tab

    你会发现,上面自己写的tab.py模块只能在当前目录下导入,如果想在系统的何何一个地方都使用怎么办呢? 此时你就要把这个tab.py所在的目录放到系统全局环境变量目录里,或者把tab.py放到 ../site-packages 目录下,这个目录在不同的OS里放的位置不一样,用 print(sys.path) 可以查看python环境变量列表。一般第三方库或自己安装的库一般情况下会放在此位置。

  • 相关阅读:
    EnterpriseLibrary
    如何只保证窗口只打开一次[即只运行一个进程]
    设计模式之工厂方法模式
    设计模式之代理类
    asp.net mvc应用架构的思考--Unity的应用及三层代码
    为什么我说不要用TransactionScope
    Redis入门学习
    实战分层架构
    asp.net mvc 4多级area实现技巧
    jsonp其实很简单【ajax跨域请求】
  • 原文地址:https://www.cnblogs.com/wangcx/p/6688842.html
Copyright © 2011-2022 走看看