zoukankan      html  css  js  c++  java
  • Python全栈之路-Day33

    1 time模块

    
    #!/usr/bin/env python
    # __Author__: "wanyongzhen"
    # Date: 2017/4/7
    
    import time
    
    # print(help(time))  # 打印time模块的帮助
    
    # print(time.time()) # 返回当前时间戳
    
    # time.sleep(3)  # sleep 3秒
    
    # print(time.clock()) # 计算CPU执行时间
    #
    # print(time.gmtime()) # 返回结构化UTC时间 不常用
    #
    # print(time.localtime())  # 返回本地时间 常用
    
    # struct_time = time.localtime()
    # print(time.strftime('%Y-%m-%d %H:%M:%S',struct_time))  # 转换成字符串时间  struct_time默认就是time.localtime()时间
    
    # print(time.strptime('2016-09-08 18:48:35','%Y-%m-%d %H:%M:%S'))   # 转换成格式化时间
    # struct_time = time.strptime('2016-09-08 18:48:35','%Y-%m-%d %H:%M:%S')
    # print(struct_time.tm_year)
    # print(struct_time.tm_mon)
    # print(struct_time.tm_mday)
    
    # print(time.ctime(3600)) # 转换时间戳转换成可读时间
    
    # help(time.mktime)
    print(time.mktime(time.localtime())) # 转换可读时间成时间戳
    
    
    import datetime
    
    print(datetime.datetime.now())  # 打印精确时间
    

    2 random模块

    #!/usr/bin/env python
    # __Author__: "wanyongzhen"
    # Date: 2017/4/8
    
    import random
    
    # print(random.random())  # 0 - 1 的随机数,不包括 0 和 1
    #
    # print(random.randint(1,8))  # 1 - 8 的随机数,包括1和8
    #
    # print(random.choice('hello')) # 随机生成hello字符串的某个元素
    #
    # print(random.choice(['Python','C++',['Java','PHP']]))  # 随机生成list的某个元素
    
    language = ['Python','C++',['Java','PHP']]
    random.shuffle(language)  # 打乱一个有序序列
    print(language)
    
    print(random.sample(language,2))  # 随机生成多个值
    
    print(random.randrange(1,3)) # 1 - 3 的随机整数 包含1 不包含3
    
    # def v_code():  # 生成一个随机的六位数
    #     code = ''
    #     for i in range(6):
    #         add_num = random.randrange(10)
    #         code += str(add_num)
    #     print(code)
    # v_code()
    
    def v_code():  # 生成一个随机的六位数
        code = ''
        for i in range(6):
        #     if i < random.randint(0,5):
        #         add_element = random.randrange(10)
        #     else:
        #         add_element = chr(random.randrange(65,91))
            add_element = random.choice([random.randrange(10),chr(random.randrange(65,91)),chr(random.randrange(97,122))])
            code += str(add_element)
        print(code)
    
    v_code()
    

    3 hashlib模块

    
    #!/usr/bin/env python
    # __Author__: "wanyongzhen"
    # Date: 2017/4/6
    
    import hashlib  # 主要用于加密 算法:md5 sha等等
    
    # # md5 加密
    # m=hashlib.md5() # hashlib.md5() 返回一个class类型
    # print(m)
    # m.update('hello world'.encode('utf8')) # 5eb63bbbe01eeed093cb22bb8f5acdc3
    # print(m.hexdigest())
    # m.update('alex'.encode('utf8'))
    # print(m.hexdigest())  # 82bb8a99b05a2d8b0de2ed691576341a
    #
    # # 与上面得到的结果相同
    # m2=hashlib.md5()
    # m2.update('hello worldalex'.encode('utf-8'))
    # print(m2.hexdigest()) # 82bb8a99b05a2d8b0de2ed691576341a
    
    # sha 加密 与md5加密调用原理相似
    # s=hashlib.sha256()
    # print(s)
    # s.update('hello world'.encode('utf8'))
    # print(s.hexdigest())
    

    4 os模块

    
    #!/usr/bin/env python
    # __Author__: "wanyongzhen"
    # Date: 2017/4/6
    
    import os   # 调用系统命令完成任务的一个模块
    
    # print(os.getcwd())  # 获取当前工作目录
    
    # os.chdir('/Users/wanyongzhen/PycharmProjects/python4期/')  # 切换工作目录
    print(os.getcwd())
    
    # print(os.curdir)   # 返回当前目录:(.)
    # print(os.pardir)   # 返回当前目录的父级目录:(..)
    
    # os.makedirs('./Day21/os/module/test')   # 相当于:mkdir -p ./Day21/os/module/test
    
    # os.removedirs('./Day21/os/module/test')  # 递归删除空目录 相当于:rmdir ./Day21
    
    # os.mkdir('./Day21')   # 相当于:mkdir ./Day21
    #
    # os.rmdir('./Day21')   # 相当于:rmdir ./Day21
    
    # print(os.listdir('../'))  # 相当于ls,返回值是list数据类型
    
    # os.remove('./test.txt')   # 相当于:rm -f ./test.txt 只能删除文件
    
    # os.rename('./test.txt','./test_rename.txt')  # 相当于:mv ./test.txt ./test_rename.txt
    # os.rename('./test','./test_rename')    # 相当于:mv ./test ./test_rename
    
    # print(os.stat('./'))  # 获取文件状态  返回类型是class
    # print(os.stat('./os_module.py'))  # 获取目录状态 返回类型是class
    # info_file=os.stat('./os_module.py')
    # info_dir=os.stat('./')
    # print(info_file.st_size)  # 文件大小
    # print(info_dir.st_size)   # 目录大小
    # print(info_file.st_mtime) # 文件修改时间
    # print(info_dir.st_mtime)  # 目录修改时间
    
    # print(os.sep)   # 获取路径分隔符
    
    # print(os.linesep) # Mac:"
    " Linux:"
    " Windows:"
    "
    
    # print(os.pathsep) # 输出用于分割文件路径的字符串
    
    # print(os.name) # 用于输出当前系统平台
    
    # print(os.system('ls')) # 执行shell命令
    
    # print(os.environ) # 打印环境变量  返回值是一个字典
    
    # print(os.path.abspath('./os_module.py'))  # 将当前目录转换成绝对路径
    
    # print(os.path.split('./os_module.py'))    # 将路径分割成目录和文件名  返回值是一个元组
    
    # print(os.path.dirname('/Users/wanyongzhen/PycharmProjects/python4期/Day20/os_module.py'))  # 获取文件所在目录名
    #
    # print(os.path.basename('/Users/wanyongzhen/PycharmProjects/python4期/Day20/os_module.py'))  # 获取文件名
    #
    # print(os.path.exists('./os_module.py')) # 如果文件或目录存在,则返回True 否则返回False
    #
    # print(os.path.isabs('./os_module.py'))  # 如果路径是一个绝对路径则返回True
    #
    # print(os.path.isfile('./os_module.py')) # 如果路径是一个文件并且该文件存在,则返回True 否则返回False
    #
    # print(os.path.isdir('./os_module.py'))  # 如果路径是一个目录并且该目录存在,则返回True 否则返回False
    
    # print(os.path.join('path1','path2','path3'))  # 将多个路径组合后返回
    
    # print(os.path.getatime('./os_module.py'))   # 返回指定的文件或目录的最近访问时间
    # print(os.path.getmtime('./os_module.py'))   # 返回指定的文件或目录的最近修改时间
    

    5 sys模块

    
    #!/usr/bin/env python
    # __Author__: "wanyongzhen"
    # Date: 2017/4/6
    
    import sys  # 与Python解释器进行交互
    
    # print(sys.argv)  # 获取脚本运行时传递的参数,返回值是一个list,第一个元素是文件名
    # 实现根据传递的参数执行不同功能
    # def post():
    #     print('Post OK')
    #
    # def download():
    #     pass
    #
    # if sys.argv[1] == 'post':
    #     post()
    # elif sys.argv[1] == 'download':
    #     download()
    
    
    # RETVAL=0  # 定义返回值RETVAL
    # sys.exit(RETVAL) # 退出程序并返回值 与Linux shell的 exit RETVAL 相似 0表示正常退出 非0表示非正常退出
    
    # print(sys.path)  # 打印Python所有模块的搜索路径  与Shell的PATH变量相似
    
    # print(sys.platform)  # 返回操作系统平台
    
    # 实现根据sys.platform执行不同的命令
    # import os
    # if sys.platform == 'win32':
    #     os.system('dir')
    # else:
    #     os.system('ls')
    
  • 相关阅读:
    我渴望自由和成功(愿与君共勉)
    gdb使用 和core dump文件调试
    谷歌浏览器快捷键大全
    Android适屏
    BestCoder Round #33
    NAT&amp;Port Forwarding&amp;Port Triggering
    程序员应该学习的书
    java代码调用rtx发送提醒消息
    js实现excel导出
    一个跨界程序员:不务正业的这几年,我如何让自己的收入翻了十倍(转)
  • 原文地址:https://www.cnblogs.com/wanyuetian/p/6770440.html
Copyright © 2011-2022 走看看