zoukankan      html  css  js  c++  java
  • 模块初识

    Python 的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在我们先来象征性的看两个简单的。

    sys

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    
    
    import sys
    
    print(sys.path) # path 打印环境变量
    print(sys.argv) # 输出 $ python test.py hello world [
    'test.py','hello','world'] # 把执行脚本时传递的参数获取到了

    os#!/usr/bin/env python# -* - coding: utf-8 -*-

    import os
    
    os.system("df -h")  # 调用系统命令,Windows下df -h 换成dir

    cmd_res = os.system("df -h") # 只执行命令,不保存结果
    print("-->",cmd_res)

    cmd_res = os.popen("dir") # 打印的是内存对象地址,想要结果在后面加.read()
    cmd_res = os.popen("dir").read()
    print("-->",cmd_res)

    os.mkdir("new_dir") # 创建一个叫new_dir的目录

    完全结合一下

    import os,sys
    
    
    # 把用户的输入的参数当做一条命令交个os.sys
    
    os.system(''.join(sys.argv[1:]))  

    自己写个模块

    import sys
    import readline
    import rlcompleter
    
    if sys.platfrom == 'darwin' and sys.version_info[0] == 2:
         readline.parse_and_bind("bind^I r1_complete")
    else:
         readline.parse_and_bind("tab:complete") #linux and python3 on mac
    #!/usr/bin/env pyton
    #python startup file
    import sys
    import readline
    import rlcompleter
    import atexit
    import os
    # tab complation
    readline.parse_adn_bind('tab: complete')
    # history file
    histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    try:
         readline.read_history_file(histfile)
    except IOError:
         pass
    atexit.register(readline.write_history_file, histfile)
    del os, histfile, readline, rlcompleter

    写完保存后就可以使用了

    localhost:~ jieli$ python
    Python 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 darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tab

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

  • 相关阅读:
    17.1.2?Replication Formats 复制格式:
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment 引入额外的Slaves 到一个存在的复制
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment 引入额外的Slaves 到一个存在的复制
    mysql读写分离
    mysql读写分离
    curl 返回响应头
    01_什么是Elasticsearch
    01_什么是Elasticsearch
    perl 自动发产品
    Jzoj1164求和
  • 原文地址:https://www.cnblogs.com/lzhn/p/7800733.html
Copyright © 2011-2022 走看看