模块
os模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cdos.curdir 返回当前目录: ('.')os.pardir 获取当前目录的父目录字符串名:('..')os.makedirs('dir1/dir2') 可生成多层递归目录os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirnameos.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirnameos.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印os.remove() 删除一个文件os.rename("oldname","new") 重命名文件/目录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不存在,返回Falseos.path.isabs(path) 如果path是绝对路径,返回Trueos.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回Falseos.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回Falseos.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间time模块
import time,datetime
print(time.time()) #时间戳
# time.sleep(3)
print(time.clock()) #cup执行的时间
now_time = time.gmtime() #结构化时间time.struct_time(tm_year=2017, tm_mon=10, tm_mday=23, tm_hour=5, tm_min=30, tm_sec=30, tm_wday=0, tm_yday=296, tm_isdst=0)
print(now_time)
bendi_time = time.localtime() #本地的结构化时间
print(bendi_time)
print(time.strftime("%Y--%m--%d %H:%M:%S"))#格式化时间
print(time.strptime("2017--10--23","%Y--%m--%d")) #转换成结构化时间
a = time.strptime("2017--10--23","%Y--%m--%d")#将结构化时间赋值给变量
print(a.tm_year) #取时间的值2017
print(time.ctime()) #Mon Oct 23 14:04:00 2017
print(datetime.datetime.now()) #2017-10-23 14:04:00.903191
random模块
import random
print(random.random()) #0到1的随机数
print(random.randint(0,5)) #0-5得数包括5
print(random.choice(["wy",1,2,3,4,[1,2,3]]))
# print(random.shuffle())
print(random.sample(["wy",1,2,3,4,[1,2,3]],2))#随机取两个值
print(random.randrange(1,2)) #不包括2
两种随机数获取def v_code():
code = ''
for i in range(4):
num = random.randrange(5)
if num ==3or num == 4:
a = str(random.randrange(0, 10))
else:
a= chr(random.randrange(65, 91))
code =code+a
print(code)
2#########def code():
a =""
for i in range(4):
c = random.choice([str(random.randrange(10)),chr(random.randrange(65,91))])
a +=c
print(a)
第二种比较简单 都能排除指定位置是数字或者是字母
hashlib加密模块
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
mport hashlib
m = hashlib.md5()
print(m)
m.update('hello'.encode('utf-8'))
print(m.hexdigest())
m.update("word".encode("utf-8"))
print(m.hexdigest())
m2 = hashlib.md5()
m2.update("helloword".encode("utf-8"))
print(m2.hexdigest())
### sha1######
m_sha1 = hashlib.sha1()
m_sha1.update("hello".encode("utf-8"))
print(m_sha1.hexdigest())
### sha256 #####
m_sha256 =hashlib.sha256()
m_sha256.update("hello".encode("utf-8"))
print(m_sha256.hexdigest())![]()

浙公网安备 33010602011771号