Python的模块(二)

一、OS模块的补充

1.os.path.abspath('..\\day3')     #取绝对路径(相对路径换绝对路径),路径会按照当前系统的方式, 一个点是当前目录, 两个点是上一级目录

2.os.listdir(e:\\spz\\day3)   #获取目录下的文件

3.os.chdir('e:\\spz\\day3')  #更改当前工作目录

4.os.system('ipconfig')   #用来执行操作系统命令,但是只能帮你执行,获取不到结果

5.os.popen('ipconfig').read()  #用来执行操作系统命令,获取到执行后结果

二、datetime模块

1.datetime.date.today()   #当天的日期,只有日期

2.datetime.datetime.today()  #当天的时间,有日期有时间

3.datetime.date.today()+datetime.timedelta(days=-2)    #取几天前或几天后的日期,-2就是取两天前的

4.res = datetime.datetime.today() + datetime.timedelta(hours=-10,minutes=-20)

res.time()  #只取到时间

res.date()  #只取到日期

res.timestamp())  #时间戳

res.strftime('%Y-%m %H:%M:%S')  #取到格式化好的时间

三、random模块

1.random.random()  #取小于1的随机的小数

2.random.randint(1,10))  #取随机整数,可指定范围,包含头尾的

3.random.choice(x))  #随机在x中选择一个,只选择一个

4.random.sample(x,9)) #随机在x中选择9个值,返回的是一个list

5.random.uniform(8.5,8.8))  #指定一个范围,然后取一个随机小数

四、nnlog模块(写日志)

1.my_log=nnlog.Logger('yuze.log',when='S',backCount=5)  #只保留5个日志文件,日志文件按日期到秒来

2.my_log.debug('这是debug的')

3.my_log.info("info级别的")

4.my_log.warning("waring 级别的")  

5.my_log.error("出错了 级别的")

五、yagmail发邮件模块

useraccount='[email protected]'

password='xxxx'     #邮箱的授权码

mail=yagmail.SMTP(user=useraccount,password=password,host='smtp.qq.com',smtp_ssl=True)   #smtp_ssl=True,如果是qq邮箱加上这个安全协议的

mail.send(to=['[email protected]','[email protected]'],cc='[email protected]',subject='吃了吗?',contents='哈哈',attachments=r'C:\Users\xxx\1.py')

六、hashlib加密模块

def myMd5(s):

  s = str(s)

  m = hashlib.md5(s.encode())  #md5加密必须得传一个bytes类型的,用encode()

  return m.hexdigest()   #获取到加密后的结果

猜你喜欢

转载自www.cnblogs.com/yz-test/p/9320400.html