python模块的学习

 1 # time 模块
 2 import time
 3 print(time.time())      #当前的时间挫
 4 #time.sleep(3)       #休息3秒钟,这3秒cpu不工作的
 5 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 英国时间
 6 #print(time.clock()) # use time.perf_counter or time.process_time
 7 print(time.perf_counter())
 8 print(time.process_time())
 9 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)
10 #以上的结构化时间就是元组的格式
11 d = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
12 print(d)    #2018-06-21 19:11:46
13 p = time.strptime('2018-06-21 19:11:46','%Y-%m-%d %H:%M:%S')    #转化为结构化时间
14 #结构化时间的作用 可以取具体的某一部分时间
15 print(p.tm_hour) #可以只取小时
16 print(time.ctime(time.time()))  #将具体的时间戳转化为时间
17 print(time.mktime())    #将具体的时间格式转化为时间戳
18 #help()查看具体的方法
19 
20 import datetime
21 print(datetime.datetime.now())  #友好的时间展示
1 import random
2 print(random.random())
3 print(random.randint(1,8))  # 包括8点的随机1-8的整数
4 print(random.choice("abcdefg"))     #字符串进行随机
5 print(random.sample(["22",[1,2,3],123,"yuan"],2))   #参数 2 随机生产出2个模块的东西  #[123, '22']
6 print(random.randrange(1,3))    #不包括3 进行随机
7 chr(65)     #将数字转化为ascii码对应的字符 65-91  总共有
1 import random
2 #chr() 将数字转化为字符
3 # 实现, 数字与字符窜的随机组合
4 code = ''
5 for i in range(5):
6     add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
7     code += str(add)    #将数字转化为字符串进行拼接
8 print(code)
 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
 2 os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
 3 os.curdir  返回当前目录: ('.')
 4 os.pardir  获取当前目录的父目录字符串名:('..')
 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录
 6 os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
 7 os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
 8 os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
 9 os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
10 os.remove()  删除一个文件
11 os.rename("oldname","newname")  重命名文件/目录
12 os.stat('path/filename')  获取文件/目录信息  文件的修改时间与访问时间都在这个信息里面
13 os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
14 os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
15 os.pathsep    输出用于分割文件路径的字符串
16 os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
17 os.system("bash command")  运行shell命令,直接显示
18 os.environ  获取系统环境变量
19 os.path.abspath(path)  返回path规范化的绝对路径
20 os.path.split(path)  将path分割成目录和文件名二元组返回
21 os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
22 os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
23 os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
24 os.path.isabs(path)  如果path是绝对路径,返回True
25 os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
26 os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
27 os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
28 os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
29 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

1 # r 可以将字符窜中的特殊字符 如 /n 换行符号
1 sys.argv           命令行参数List,第一个元素是程序本身路径 ['文件名','post/get',path]
2 sys.exit(n)        退出程序,正常退出时exit(0)
3 sys.version        获取Python解释程序的版本信息
4 sys.maxint         最大的Int值
5 sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 自己写的模块:sys.path.append()将自己写的模块添加到环境变量中进行调用
6 sys.platform       返回操作系统平台名称
7 sys.stdout.write('please:')  #终端的标准输出
8 val = sys.stdin.readline()[:-1]

 1 #shutil 模块 

直接参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html 

用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

1 import md5
2 hash = md5.new()
3 hash.update('admin')
4 print hash.hexdigest()
1 import sha
2 
3 hash = sha.new()
4 hash.update('admin')
5 print hash.hexdigest()
 1 import hashlib
 2  
 3 # ######## md5 ########
 4  
 5 hash = hashlib.md5()
 6 hash.update('admin')
 7 print hash.hexdigest()
 8  
 9 # ######## sha1 ########
10  
11 hash = hashlib.sha1()
12 hash.update('admin')
13 print hash.hexdigest()
14  
15 # ######## sha256 ########
16  
17 hash = hashlib.sha256()
18 hash.update('admin')
19 print hash.hexdigest()
20  
21  
22 # ######## sha384 ########
23  
24 hash = hashlib.sha384()
25 hash.update('admin')
26 print hash.hexdigest()
27  
28 # ######## sha512 ########
29  
30 hash = hashlib.sha512()
31 hash.update('admin')
32 print hash.hexdigest()

 1 #以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。 

1 import hashlib
2  
3 # ######## md5 ########
4  
5 hash = hashlib.md5('898oaFs09f')
6 hash.update('admin')  #这里为你想转化的明文   python3.0 需要encode utf8
7 print hash.hexdigest()  #通过该方法转化成密文
#还不够吊?python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密
1 import hmac
2 h = hmac.new('wueiqi')
3 h.update('hellowo')
4 print h.hexdigest()

 #很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug(), info(), warning(), error() and critical() 5个级别,下面我们看一下怎么用。 

1 import logging
2  
3 logging.warning("user [alex] attempted wrong password more than 3 times")
4 logging.critical("server is down")
5  
6 #输出
7 WARNING:root:user [alex] attempted wrong password more than 3 times
8 CRITICAL:root:server is down

更多模块的学习查看下面的链接,多联系

更加详细说明查看链接:http://www.cnblogs.com/alex3714/articles/5161349.html

猜你喜欢

转载自www.cnblogs.com/neilyoung22/p/9211381.html