• [Python_3] Python 函数 & IO



    0. 说明

      Python 函数 & IO 笔记,基于 Python 3.6.2

      参考

       Python: read(), readline()和readlines()使用方法及性能比较

       Python3 File(文件) 方法

       Python基础

       


    1. 函数

    # -*-coding:utf-8-*-
    
    """
        函数
    """
    
    # 定义函数,有return语句,否则返回None
    def add(a, b):
        # 有返回语句
        print("a : " + str(a))
        print("b : " + str(b))
        return a + b
    
    
    print(add(2, 3))
    
    """
        定义函数,有return语句,否则返回None
        *a : 变长参数
        *args : 固定写法,表示当前位置上任何多个无名参数,它是一个tuple
        **kwargs: 固定写法,关键字参数,它是一个dict
        此种方式类似于Java 的反射中的 Method 类,能够提取函数的运行时信息。
    """
    
    
    def f1(*a):
        for e in a:
            print(e)
    
    
    # 调用函数,传递变长参数
    f1((1, 2, 3, 4, 5))
    
    
    def f2(a, b, c, *args):
        print(str(args))
    
    
    f2(1, 2, 3, 4, 5)
    
    
    def foo(x, *args, **kwargs):
        print('args=', args)
        print('kwargs=', kwargs)
        print('**********************')
    
    
    foo(1, 2, 3, 4)
    foo(1000, a=1, b=2, c=3)
    foo(1, 2, a=4, b=5, c=100)

    2. IO 

    # -*-coding:utf-8-*-
    
    """
        IO
    """
    
    """
        文件读操作
    """
    # 一次性读取所有行文件
    f1 = open("e:/data.txt")
    lines = f1.readlines()
    for l in lines:
        print(l, end="")
    f1.close()
    
    # 每次读取下一行文件
    print()
    print("=============")
    f2 = open("e:/data.txt")
    while (True):
        # 读取第一行
        line = f2.readline()
        while line is not None and line != "":
            print(line, end="")
            # 读取下一行
            line = f2.readline()
        else:
            break
    f2.close()
    
    
    """
        None,类似于 Java 中 null 的表示不存在。
    """
    x = None
    print(x)
    
    
    """"
        文件写操作
        写入文件 mode=r | wa |
        w : overwrite 覆盖模式
        a : append 追加模式
    """
    f = open("e:/data2.txt", mode="a")
    f.write("i am panda")
    f.close()
    
    
    """
        文件重命名
        源文件必须存在
    """
    import os
    os.renames("e:/data2.txt", "e:/data3.txt")
    
    """
        删除文件
    """
    import os
    os.remove("e:/data3.txt")
    
    """
        创建 & 删除空目录
    """
    import os
    # os.mkdir("e:/testdir")
    os.removedirs("e:/testdir")
    
    """
        列出目录的元素
    """
    import os
    files = os.listdir("d:/")
    for i in files:
        print(i)

    3. 主函数运行 

    # -*-coding:utf-8-*-
    
    """
        导入 Python 模块,每个 Python 文件就是一个模块
        判断当前文件是否直接运行,还是被其他引用
        直接运行的时候值为"__main__"
    """
    import test6_function
    test6_function.add(1, 2)
    print(__name__)
    
    if __name__ == "__main__":
        print(100)

    4. 参数提取 

      模拟参数设置

      

    """
        参数提取
        通过 sys 的 argv 属性提取参数列表
    """
    # 提取脚本的参数
    import sys
    
    r = sys.argv
    print(r[0])
    print(r[1])

      结果如下,第一个参数为脚本


    5. 反射访问 

    """
        反射访问
    """
    s = "xxx"
    s.__len__()
    # 返回对象的指定属性,没有的话,可以指定默认值
    r1 = getattr(s, "len", "no this attr")
    r2 = getattr(s, "__len__", "no this attr")
    print(r1)
    print(r2)

    6. 日期函数 

    """
        时间函数
    """
    # 导入时间库
    import datetime
    datetime.datetime.now()
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

  • 相关阅读:
    NGINX之——配置HTTPS加密反向代理訪问–自签CA
    AVPlayer的使用,带缓冲
    优化数据页面(15)——表题应当准确精练
    FFmpeg基础库编程开发学习笔记——音频常见格式及字幕格式
    【版本号公布】Jeecg-P3 1.0 公布,J2EE微服务框架(插件开发)
    猫猫学iOS之UILabel设置圆角不成功所做调控更改
    linux strace-跟踪进程的系统调用或是信号产生情况,lstrace-跟踪己丑年调用库函数情况,进程跟踪调试命令
    text
    sql server 2008 开启1433端口,开启远程连接
    openStack kvm 虚拟机CPU颗粒化控制
  • 原文地址:https://www.cnblogs.com/share23/p/9821041.html
Copyright © 2020-2023  润新知