zoukankan      html  css  js  c++  java
  • python学习笔记(七)-- 随机数random模块,os模块、time模块

    随机数random

    import random
    random.randint(1,100)#1到100的整数
    print(random.sample(['a','b','d'],2))#随机从里面取几个元素,后面的参数是数字
    l = [1,2,3,4,5,6,7,8,9]
    print(random.choice(l))#随机取一个元素
    print(random.uniform(1,19))#指定范围,取随机的小数
    random.shuffle()#洗牌,没有返回值

    os模块

    import os
    os.rename('old','new')#改名
    os.remove('a.txt')#删除文件
    print(os.listdir('c:python36'))#列出某个目录下所有文件,默认为当前目录
    print(os.getcwd())#获取当前路径
    os.chdir('../day5')#改变当前目录,..为返回上一级目录 
    os.chdir(r'C:UsersAdministratorDesktop')#绝对路径,前面加r可以不用转义
    print(os.path.getsize('a.txt'))#获取文件大小
    os.mkdir('abc')#创建文件夹,父目录不存在会报错
    os.makedirs('xxx/abc1')#创建文件夹,父目录不存在自动创建
    res = os.path.exists('xxx/abc2')#判断文件是否存在
    print(res)
    res1 = os.path.isdir()#判断是否是文件夹
    res2 = os.path.isfile()#判断是否是文件
    
    res = os.system('dir')#执行操作系统命令,但是取不到返回结果
    result = os.popen('ipconfig').read()#执行操作系统命令,并返回结果
    print(os.cpu_count())#cpu个数
    result = os.path.join('test','case','abc.txt')#连接路径
    os.path.split(r'e:abccda.txt')#把路径分隔成路径和文件名
    result = os.path.abspath('../day5')#根据相对路径获取绝对路径
    p = r'e:abccda.txt'
    print(os.path.dirname(p))#取父目录,结果是e:abccd
    
    # os.path.getctime()#文件创建时间
    # os.path.getatime()#文件最后访问时间
    # os.path.getmtime()#文件最后修改时间
    
    #os.walk()遍历目录后返回多个list,
    #分别为路径、目录、文件
    for path,dirs,files in os.walk(r'c:'):
        print(paths)#当前路径
        print(dirs)#当前路径下的所有目录
        print(files)#当前路径下的所有文件
        for f in files:
            if '测试' in f:
                real_path = os.path.join(cur_dir,f)
                print(real_path)

     time模块

    import time
    print(time.time())#获取当前的时间戳
    
    #计算三天前的时间
    #首先得到当前时间戳,然后减去三天的秒数
    t = time.time() - 86400 -86400 -86400
    
    #取当前格式化时间
    #%Y-%m-%d %H:%M:%S' 时分秒全是大写的,中间间隔可以更改可以不要
    res = time.strftime('%Y-%m-%d %X')#时间格式 2019-04-27 14:47:18
    
    #1、时间戳转换成格式化好的时间
    # time_tumple = time.localtime(1556088065)#把时间戳转化成时间元组,取的当地时间
    time_tumple = time.gmtime(1556088065)#把时间戳转化成时间元组,取的标准时间,与中国差八个小时
    result = time.strftime('%Y-%m-%d %H:%M:%S',time_tumple)#格式化时间元组
    
    #2、格式化时间转成时间戳
    time_tumple3 = time.strptime('19980323172259','%Y%m%d %H%M%S')#把格式化时间转成时间元组
    result3 = time.mktime(time_tumple3)#把时间元组转成时间戳
    
    #封装时间戳与格式化时间相互转换
    def str_to_timestamp(str_time=None,format='%Y%m%d %H%M%S'):
        if str_time:
            time_tuple = time.strptime(str_time,format)#格式化的时间转化时间元组
            result = time.mktime(time_tuple)#时间元组转化时间戳
            return int(result)
        return int(time.time())
                         
    def timestamp_to_str(timestamp=None,format='%Y%m%d %H%M%S'):
        #时间戳转格式化的时间,如果没有传时间戳,就获取当前的格式化时间
        if timestamp:
            time_tuple = time.localtime(timestamp)#把时间戳转换成时间元组
            result = time.strftime(time_tuple,format)
            return result
        else:
            return time.strftime(format)
    print(str_to_timestamp())
  • 相关阅读:
    Node(十一)mongoose配合Node实现注册登录(注册上传头像,登录成功后显示用户信息)
    JS案例:购物车操作(简单实现)
    JS案例:Jq中的fadeOut和fadeIn实现简单轮播(没完善,简单实现)
    JS案例:小球拖动,记录轨迹,并原路返回
    html框架frame iframe
    单元测试
    软件测试计划、依据、规范
    软件测试
    html表单
    html块 布局
  • 原文地址:https://www.cnblogs.com/yanyan-/p/10750175.html
Copyright © 2011-2022 走看看