系统交互-常见库
系统操作-os
os库(operating system,提供操作系统函数)
常用的文件、路径
os.path.dirname(文件名字): 参数为文件名字,输出路径名,/Users/lr_work/liurong_sqls
os.path.abspath(文件名字): 参数为文件名,输出完整文件名,/Users/lr_work/liurong_sqls/run.py
os.getcwd() : 无参数,路径名字,/Users/lr_work/liurong_sqls
__file__ : 文件名字,输出/Users/lr_work/liurong_sqls/run.py
sys.argv[0]: 文件名字,输出/Users/lr_work/liurong_sqls/run.py
os.path.exists(路径名) : 返回True/False
os.path.join(路径1,路径2): 返回路径1+路径2,如果路径2的起始字符为‘/’,则返回结果为路径2
os.curdir : 输出当前路径,"."
1. __file__是什么?
ans:当前文件的名字。
例如r.py内容如下
import os
if __name__ == "__main__":
print __file__
输出结果:r.py
2. print __file__输出结果是固定的吗?
ans:不是固定的。pycharm中会做改动,pycharm会私自加上一个完整路径名字。
直接执行文件输出文件名字,例如:
➜ 20181018 python r.py
r.py
从pycharm中编辑完文本,并在pycharm中运行时,结果为:
➜ 20181018 python run.py
/Users/liurong07/Documents/code/QA/20181018/run.py
3. 如何获取文件的完整路径?
os.path.abspath(__file__)
输出结果:/Users/liurong07/Documents/code/QA/20181018/run.py
4.如何获取py文件的当前目录?
方法1: os.getcwd()
方法2: os.path.dirname(os.path.abspath(__file__)),推荐此方法。
5. 运行完当前py文件后,py文件的当前目录会增加到sys.path中。
6. 路径拼接 os.path.join(path_1, path_2)
例如
>>> path_1 = '/home/work'
>>> path_2 = 'case/log'
>>> path_1
'/home/work'
>>> path_2
'case/log'
>>> os.path.join(path_1, path_2)
'/home/work/case/log'
如果两个目录中间/重叠呢? --无法正确拼接,只得到第2个参数的路径
>>> path_3
'/home/work/'
>>> path_4
'/case/log'
>>> os.path.join(path_3, path_2)
'/home/work/case/log'
7. 判断某一个路径是否存在 os.path.exists(path_1)
举例
>>> import os
path为存在的目录
>>> path = '/Users/liurong07/Documents/code/QA/20181018'
>>> print os.path.exists(path)
True
path2为不存在的目录
>>> path2 = '/Users/liurong07/Documents/code/QA/20181018/xxxx'
>>> print os.path.exists(path2)
False
8. 如果某个目录不存在,如果创建目录?
执行linux命令,使用os.system,如下:
>>> os.system('mkdir -p ' + path2)
0
>>> print os.path.exists(path2)
True
9.获取当前文件名
两者等效
sys.argv[0]
__file__
10.获取当前文件的路径
两者等效
os.getcwd()
os.path.dirname(__file__)
环境变量-dotenv
dotenv库是干啥用的? 一句话:加载环境变量的,让代码和敏感配置分离,提升安全。
配置信息写入.env文件中
APP_ID=cli_9fxxxx00b APP_SECRET=EX6xxxxOF
用法举例
# load env parameters form file named .env load_dotenv(find_dotenv())
import os
# load from env
APP_ID = os.getenv("APP_ID")
APP_SECRET = os.getenv("APP_SECRET")
日志-logging
logging库提供日志打印功能。值得一提的是,不仅能打印到日志文件,还能打印到控制台。
日志级别
logging一共分为5个级别,从低到高依次为: DEBUG<INFO<WARNING<ERROR<CRITICAL
日志参数配置
配置接口logging.basicConf()
参数详解
控制日志的级别
level=logging.DEBUG (或者: logging.INFO / logging.WARNING / logging.ERROR / logging.CRITICAL)
假如level=logging.WARNING, 那么低于warning级别的日志都不会打印了。
设置日志名字(也设置了日志存放的路径)
filename="%s/test.log" % "/home/work"
设置文件模式
什么是文件模式呢?
filemode='w' (或者'a')
'w'表示什么意思?
'a'表示什么意思?
设置日志格式
控制了每一行日志都输出哪些字段
format="%(levelname)s-%(asctime)s-%(filename)s-%(funcName)s-%(lineno)d-%(message)s"
其中每个字段什么意思呢,可以参考下面
日志级别
%(levelno)s 日志级别数值
%(levelname)s 日志级别名字
%(asctime)s 日志打印时间
%(filename)s 文件名称
%(funcName)s 函数名称
%(lineno)d 行号
%(process)d 进程ID
%(thread)d 线程ID
%(threadName) 线程名称
%(message)s 打印日志信息
demo
(demo-1) 将日志输出在控制台
import logging
logging.basicConfig(level=logging.INFO,
filemode='a',
format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s")
if __name__ == "__main__":
logging.debug("this is debug message")
logging.info("this is info message")
logging.warning("this is warning message")
logging.error("this is error message")
logging.critical("this is critical message")
输出结果
[INFO][2018-10-19 11:05:10,013]run.py-29 this is info message
[WARNING][2018-10-19 11:05:10,013]run.py-30 this is warning message
[ERROR][2018-10-19 11:05:10,013]run.py-31 this is error message
[CRITICAL][2018-10-19 11:05:10,013]run.py-32 this is critical message
Process finished with exit code 0
(demo-2) 将日志输出在日志文件
logging.basicConfig(level=logging.INFO,
filename="%s/run_info.log" % LOG_PATH,
filemode='a',
format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s")
if __name__ == "__main__":
logging.debug("this is debug message")
logging.info("this is info message")
logging.warning("this is warning message")
logging.error("this is error message")
logging.critical("this is critical message")
输出结果
➜ log cat /Users/liurong07/Documents/code/QA/20181018/log/run_info.log
[INFO][2018-10-19 11:07:34,372]run.py-25 this is info message
[WARNING][2018-10-19 11:07:34,373]run.py-26 this is warning message
[ERROR][2018-10-19 11:07:34,373]run.py-27 this is error message
[CRITICAL][2018-10-19 11:07:34,374]run.py-28 this is critical message
数据库交互-MySQLdb
MySQLdb库简介
提供mysql的基本操作(包括建表,读取表数据,插入数据到表)
数据库操作基本步骤
mysql基本读取举例
输出结果
View Code
简单demo
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
DB_HOST = "120.92.81.22"
DB_PORT = 55219
DB_USER = "admin"
DB_PASSWORD = "LIUxrong123@"
def cyc_execute(cmd, num):
# 打开数据库
db = MySQLdb.connect(host=DB_HOST, port=DB_PORT, user=DB_USER, passwd=DB_PASSWORD, charset='utf8')
# 获取游标
cursor = db.cursor()
# 循环执行sql
for i in range(0, num):
# 执行sql
cursor.execute(cmd)
# 获取数据
data = cursor.fetchone()
# 打印结果
print "Database version : %s " % data
# 关闭数据库
db.close()
if __name__ == "__main__":
sql_cmd = "select user();"
num = 10
cyc_execute(sql_cmd, num)
数学-常见库
随机函数-random
Q:想生成一个随机整数,范围在[0, 100]之内,怎么弄?
>>> random.randint(0, 100) 7
Q:想生成一个随机整数,范围在[0, 100]之内,而且能够被5整除的数,怎么弄?
>>> random.randrange(0, 100, 5) 0 >>> random.randrange(0, 100, 5) 60 >>> random.randrange(0, 100, 5) 10 >>> random.randrange(0, 100, 5) 75
Q:如何生成一个随机字符?
>>> random.choice('abc_-#')
'b'
>>> random.choice('abc_-#')
'_'
>>> random.choice('abc_-#')
'#'
>>> random.choice('abc_-#')
'_'
>>> random.choice('abc_-#')
'#'
>>> random.choice('abc_-#')
'#'
>>> random.choice('abc_-#')
'c'

浙公网安备 33010602011771号