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

    模块,也叫库,分为标准库和第三方库。标准库,直接导入使用,比如import getpass;第三方库,需下载安装才能使用,比如paramiko;

    一、sys模块

    import sys  
    print(sys.path) 打印环境变量
    ['C:\Users\HW\PycharmProjects\G3\day1-3.18', 'C:\Users\HW\PycharmProjects\G3', 'C:\Users\HW\AppData\Local\Programs\Python\Python36\python36.zip', 'C:\Users\HW\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\HW\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\HW\AppData\Local\Programs\Python\Python36', 'C:\Users\HW\AppData\Local\Programs\Python\Python36\lib\site-packages']

    其中:
    标准库:'C:\Users\HW\AppData\Local\Programs\Python\Python36'
    第三方库:'C:\Users\HW\AppData\Local\Programs\Python\Python36\lib\site-packages'

    sys.argv用法:sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径;

    所以参数从1开始.

    arg[1]表示第一个命令行参数

    arg[1][2:] 表示取第一个命令行参数,但是去掉前两个字节

    举例:

    >>> import sys,os
    >>> os.system(sys.argv[1])  #表示os.system接收命令行参数,运行参数指令

    再比如命令行参数为   “--help” ,就去掉“--”,执行"help"参数。

     1import sys   
     2def readfile(filename):  #从文件中读出文件内容   
     3    '''''Print a file to the standard output.'''  
     4    f = file(filename)   
     5    while True:   
     6        line = f.readline()   
     7        if len(line) == 0:   
     8            break  
     9        print line, # notice comma  分别输出每行内容   
    10    f.close()   
    11# Script starts from here   
    12if len(sys.argv) < 2:   
    13    print 'No action specified.'  
    14    sys.exit()   
    15if sys.argv[1].startswith('--'):   
    16    option = sys.argv[1][2:]   
    17    # fetch sys.argv[1] but without the first two characters   
    18    if option == 'version':  #当命令行参数为-- version,显示版本号   
    19        print 'Version 1.2'  
    20    elif option == 'help':  #当命令行参数为--help时,显示相关帮助内容   
    21        print '''''  
    22This program prints files to the standard output.  
    23Any number of files can be specified.  
    24Options include:  
    25  --version : Prints the version number  
    26  --help    : Display this help'''  
    27    else:   
    28        print 'Unknown option.'  
    29    sys.exit()   
    30else:   
    31    for filename in sys.argv[1:]: #当参数为文件名时,传入readfile,读出其内容   
    32        readfile(filename)  
    33import sys
    34def readfile(filename):  #从文件中读出文件内容
    35    '''Print a file to the standard output.'''
    36    f = file(filename)
    37    while True:
    38        line = f.readline()
    39        if len(line) == 0:
    40            break
    41        print line, # notice comma  分别输出每行内容
    42    f.close()
    43# Script starts from here
    44if len(sys.argv) < 2:
    45    print 'No action specified.'
    46    sys.exit()
    47if sys.argv[1].startswith('--'):
    48    option = sys.argv[1][2:]
    49    # fetch sys.argv[1] but without the first two characters
    50    if option == 'version':  #当命令行参数为-- version,显示版本号
    51        print 'Version 1.2'
    52    elif option == 'help':  #当命令行参数为--help时,显示相关帮助内容
    53        print '''
    54This program prints files to the standard output.
    55Any number of files can be specified.
    56Options include:
    57  --version : Prints the version number
    58  --help    : Display this help'''
    59    else:
    60        print 'Unknown option.'
    61    sys.exit()
    62else:
    63    for filename in sys.argv[1:]: #当参数为文件名时,传入readfile,读出其内容
    64        readfile(filename) 
    View Code

    在windows的cmd下:

    >>> import sys
    >>> print(sys.argv)
    ['']

    在pycharm下运行:

    1 import sys
    2 print(sys.argv)

    ['C:/Users/HW/PycharmProjects/G3/作业/test.py']

    二、os模块:系统创建目录,查看目录下多少个文件及文件大小等系统操作相关的

    >>> import os
    >>> cmd_res = os.system("dir")
     驱动器 C 中的卷是 系统
     卷的序列号是 C215-9FE3
    
     C:UsersHW 的目录
    
    2017/04/07  16:19    <DIR>          .
    2017/04/07  16:19    <DIR>          ..
    2016/08/31  15:30    <DIR>          .android
    2017/03/18  14:42    <DIR>          .PyCharm2016.3
    2016/10/12  22:17    <DIR>          .VirtualBox
    2016/10/23  21:49    <DIR>          .YYBox
    2016/07/03  22:19    <DIR>          Contacts
    2017/04/14  12:24    <DIR>          Desktop
    2017/02/26  22:02    <DIR>          Documents
    ...

    >>> print("--->",cmd_res)
    ---> 0          #返回0表示命令执行成功

    若想把命令执行,永久保存下来,即存到磁盘保存到文件中,

    >>> import os>>> cmd_res = os.popen("dir").read()  #os的popen方法保存dir命令执行结果,然后通过read显示出来
    >>> print("--->",cmd_res)

    三、第三方(自写)模块如何使用?比如有一个login.py模块,如何调用?

    解决办法:

  • 相关阅读:
    正则表达式的总结
    网络搭建的四种方式
    argparse的总结详情
    错误记录
    8x8点阵的原理及代码实现
    __pycache__的认识记录
    浏览器渲染原理及流程
    javascript 中 async/await 的用法
    浏览器的进程和线程
    JS 对象toString 和 valueof 方法
  • 原文地址:https://www.cnblogs.com/wolfs685/p/6718374.html
Copyright © 2011-2022 走看看