zoukankan      html  css  js  c++  java
  • Python常用模块/函数

    一、文件和目录操作

      创建、删除、修改、拼接、获取当前目录、遍历目录下的文件、获取文件大小、修改日期、判断文件是否存在等。略

    二、日期和时间(内置模块:time、datatime、calendar)

    1.time.time()  #返回自1970年1月1日0点到当前时间经过的秒数

    实例1:获取某函数执行的时间,单位秒

    import time
    before = time.time()
    func1
    after = time.time()
    print(f"调用func1,花费时间{after-before}")

    2.datetime.now() #返回当前时间对应的字符串

    from datetime import datetime
    print(datetime.now())
    输出结果:2020-06-27 15:48:38.400701

    3.以指定格式显示字符串

    datetime.now().strftime('%Y-%m-%d -- %H:%M:%S')
    time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

    三、python程序中调用其他程序

      python中调用外部程序,使用标准库os库的system函数、或者subproprocess库。

    1.wget(wget是一个从网络上自动下载文件的自由工具,支持通过 HTTP、HTTPS、FTP 三个最常见的 TCP/IP协议下载)

    1)mac上安装wget命令:brew install wget

    2)wget --help/wget -h

    3)使用wget下载文件,下载文件至当前目录下,mac终端命令:wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip

    2.os.system函数

    1)os.system调用外部程序,必须等被调用程序执行结束,才能继续往下执行

    2)os.system 函数没法获取 被调用程序输出到终端窗口的内容

    import os
    cmd = 'wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip'
    os.system(cmd)
    ---
    version = input('请输入安装包版本:')
    cmd = fr'd: oolswget http://mirrors.sohu.com/nginx/nginx-{version}.zip'
    os.system(cmd)

    3.subprocess模块

    实例1:将本该在终端输出的信息用pipe获取,并进行分析

    from subprocess import PIPE, Popen
    # 返回的是 Popen 实例对象
    proc = Popen(
    'du -sh *',
    stdin = None,
    stdout = PIPE,
    stderr = PIPE,
    shell=True)
    outinfo, errinfo = proc.communicate() # communicate 方法返回 输出到 标准输出 和 标准错误 的字节串内容
    outinfo = outinfo.decode('gbk')
    errinfo = errinfo.decode('gbk')
    outputList = outinfo.splitlines()
    print(outputList[0].split(' ')[0].strip())

    实例2:启动wget下载文件

    from subprocess import Popen
    proc = Popen(
    args='wget http://xxxxserver/xxxx.zip',
    shell=True
    )

    使用subprocess不需要等外部程序执行结束,可以继续执行其他程序

    四、多线程

    如果是自动化测试用例编写,可以使用pytest测试框架,自带多线程实现方法。



    
    
    
    
    
    





    
    




    
    
  • 相关阅读:
    iPad用户使用Mac和Windows应用软件-记Parallels Access使用体验
    用ipad维护Linux服务器
    Class Model of Quick Time Plugin
    vm_write
    [转]Getting a Packet Trace
    [原]调试没有符号的 iOS 应用
    [转]编译 JavaScriptCore For iOS
    [转]ARM64 Function Calling Conventions
    [转]如何选择职业道路
    [转]Native Java Bytecode Debugging without Source Code
  • 原文地址:https://www.cnblogs.com/lucylu/p/13212265.html
Copyright © 2011-2022 走看看