python模块的学习
# time 模块
import time
print(time.time()) #当前的时间挫
#time.sleep(3) #休息3秒钟,这3秒cpu不工作的
print(time.gmtime()) #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=10, tm_min=37, tm_sec=30, tm_wday=3, tm_yday=172, tm_isdst=0) 时间标准时间 UTC 英国时间
#print(time.clock()) # use time.perf_counter or time.process_time
print(time.perf_counter())
print(time.process_time())
print(time.localtime()) #本地时间time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=18, tm_min=41, tm_sec=50, tm_wday=3, tm_yday=172, tm_isdst=0)
#以上的结构化时间就是元组的格式
d = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print(d) #2018-06-21 19:11:46
p = time.strptime('2018-06-21 19:11:46','%Y-%m-%d %H:%M:%S') #转化为结构化时间
#结构化时间的作用 可以取具体的某一部分时间
print(p.tm_hour) #可以只取小时
print(time.ctime(time.time())) #将具体的时间戳转化为时间
print(time.mktime()) #将具体的时间格式转化为时间戳
#help()查看具体的方法 import datetime
print(datetime.datetime.now()) #友好的时间展示
import random
print(random.random())
print(random.randint(1,8)) # 包括8点的随机1-8的整数
print(random.choice("abcdefg")) #字符串进行随机
print(random.sample(["",[1,2,3],123,"yuan"],2)) #参数 2 随机生产出2个模块的东西 #[123, '22']
print(random.randrange(1,3)) #不包括3 进行随机
chr(65) #将数字转化为ascii码对应的字符 65-91 总共有
import random
#chr() 将数字转化为字符
# 实现, 数字与字符窜的随机组合
code = ''
for i in range(5):
add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
code += str(add) #将数字转化为字符串进行拼接
print(code)
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息 文件的修改时间与访问时间都在这个信息里面
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.environ 获取系统环境变量
os.path.abspath(path) 返回path规范化的绝对路径
os.path.split(path) 将path分割成目录和文件名二元组返回
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
更多参考资料:
http://www.cnblogs.com/alex3714/articles/5161349.html
https://docs.python.org/2/library/os.html?highlight=os#module-os
# r 可以将字符窜中的特殊字符 如 /n 换行符号
sys.argv 命令行参数List,第一个元素是程序本身路径 ['文件名','post/get',path]
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 自己写的模块:sys.path.append()将自己写的模块添加到环境变量中进行调用
sys.platform 返回操作系统平台名称
sys.stdout.write('please:') #终端的标准输出
val = sys.stdin.readline()[:-1]
#shutil 模块
直接参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html
用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import md5
hash = md5.new()
hash.update('admin')
print hash.hexdigest()
import sha hash = sha.new()
hash.update('admin')
print hash.hexdigest()
import hashlib # ######## md5 ######## hash = hashlib.md5()
hash.update('admin')
print hash.hexdigest() # ######## sha1 ######## hash = hashlib.sha1()
hash.update('admin')
print hash.hexdigest() # ######## sha256 ######## hash = hashlib.sha256()
hash.update('admin')
print hash.hexdigest() # ######## sha384 ######## hash = hashlib.sha384()
hash.update('admin')
print hash.hexdigest() # ######## sha512 ######## hash = hashlib.sha512()
hash.update('admin')
print hash.hexdigest()
#以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。
import hashlib
# ######## md5 ########
hash = hashlib.md5('898oaFs09f')
hash.update('admin') #这里为你想转化的明文 python3.0 需要encode utf8
print hash.hexdigest() #通过该方法转化成密文
#还不够吊?python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密
import hmac
h = hmac.new('wueiqi')
h.update('hellowo')
print h.hexdigest()
#很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug(), info(), warning(), error() and critical() 5个级别,下面我们看一下怎么用。
import logging
logging.warning("user [alex] attempted wrong password more than 3 times")
logging.critical("server is down")
#输出
WARNING:root:user [alex] attempted wrong password more than 3 times
CRITICAL:root:server is down
更多模块的学习查看下面的链接,多联系
更加详细说明查看链接:http://www.cnblogs.com/alex3714/articles/5161349.html
python模块的学习的更多相关文章
- python模块subprocess学习
当我们想要调用系统命令,可以使用os,commands还有subprocess模块整理如下: os模块: 1. os.system 输出命令结果到屏幕.返回命令执行状态. >>> o ...
- Python模块的介绍
Python模块的学习: 1.os模块: 下面只对os模块中几个比较常用的方法做一些简单的示例: os.system():这个方法在shell中体现的比较多,在dos命令行中也可以执行,下面就以在do ...
- Python模块之信号学习(signal)
信号概述 在学习Python前应该学习下Linux下的信号,软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核也可以因为内部事 ...
- 【转】Python模块学习 - fnmatch & glob
[转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...
- python模块学习第 0000 题
将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果: 好可爱>%<! 题目来源:https://github.com/Yixiao ...
- 【目录】Python模块学习系列
目录:Python模块学习笔记 1.Python模块学习 - Paramiko - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...
- Python模块——loguru日志模块简单学习
Python loguru模块简单学习 首先安装模块:pip install logoru,然后引入模块: from loguru import logger 1.直接输出到console logge ...
- 解惑Python模块学习,该如何着手操作...
Python模块 晚上和朋友聊天,说到公司要求精兵计划,全员都要有编程能力.然后C.Java.Python-对于零基础入门的,当然是选择Python的人较多了.可朋友说他只是看了简单的语法,可pyth ...
- Python入门基础学习(模块,包)
Python基础学习笔记(五) 模块的概念:模块是python程序架构的一个核心概念 每个以拓展名py结尾的python源代码文件都是一个模块 模块名同样也是一个标识符,需要符合标识符的命名规则 在模 ...
随机推荐
- FasterRCNN 提升分类精度(转)
近年来,随着深度学习的崛起,计算机视觉得到飞速发展.目标检测作为计算机视觉的基础算法,也搭上了深度学习的快车.基于Proposal的检测框架,从R-CNN到Faster R-CNN,算法性能越来越高, ...
- Caffe 分类问题 Check failed: error == cudaSuccess (2 vs. 0) out of memory
如果图片过大,需要适当缩小batch_size的值,否则使用GPU时可能超出其缓存大小而报错
- laravel项目安装
composer install 遇到的问题 composer.json 里有多余的逗号 没有打开php_openssl.php_fileinfo 报错没具体报错信息,修改配置文件 config\ap ...
- Adding ASP.NET MVC5 Identity Authentication to an existing project
Configuring Identity to your existing project is not hard thing. You must install some NuGet package ...
- laravel5.8笔记三:常用命令
创建控制器 php artisan make:controller Index/IndexController 创建模型 php artisan make:model Index/IndexContr ...
- 关于BigDecimal.ROUND_HALF_UP与ROUND_HALF_DOWN
ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1 BigDecimal a ...
- centos7上修改lv逻辑卷的大小
author:headsen chen date: 2019-03-18 15:24:22 1,查看 [root@localhost mnt]# df -h Filesystem Size Used ...
- 自闭的D7
先是一道**题然后我死了啊. A: 哇人人都会我不会系列. 我们可以运用逆向思维啊,把它转化成若干个人从点1进来, 然后我们考虑深度 dep,用 num[dep] 表示深度大于等于这个点的有多少个, ...
- centos 安装 python36
centos6 安装 python36 临时方法: https://www.softwarecollections.org/en/scls/rhscl/rh-python36/ 方法二: http:/ ...
- 协程greenlet、gevent
greenlet为了更好使用协程来完成多任务,python中greenlet模块对其封装,从而使得切换任务变得更加简单安装方式 pip3 install greenlet 示例代码: from gre ...