python常用库函数
2020-06-29 本文已影响0人
迷糊银儿
-
os.path.join(os.path.dirname(file), "./log/")
注意比较join()与os.path.join()
join() :将序列、字符串 、元组等中的元素以指定的字符连接生成一个新的字符串。
语法:str.join(sequence)
参数说明:str:指定的字符,即分隔符; sequence:需要连接的元素
image.png
os.path.join() : 将多个路径组合后返回
语法:os.path.join(path1[,path2[,……]])
image.png
-
os.path.dirname(file)
语法:os.path.dirname(path)
功能:去掉文件名,返回目录
os.path.dirname(file)的使用
(1).当"print os.path.dirname(file)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:python d:/pythonSrc/test/test.py 那么将输出 d:/pythonSrc/test
(2).当"print os.path.dirname(file)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:
python test.py 那么将输出空字符串
-
simple_jsonstr_response(-2, u"用户认证失败,请登录")
-
生成随机数
import random,numpy
print random.randint(0,1) #随机数范围[0,1]
print numpy.random.randint(0,2) # 随机数范围[0,2)
- time相关
import time
print time.time()
time_all = time.localtime(time.time())
print time_all
print '%d/%d/%d' % (time_all.tm_year, time_all.tm_mon, time_all.tm_mday)
print time.asctime( time.localtime(time.time()) )
----------------------
1593485891.53
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=30, tm_hour=10, tm_min=58, tm_sec=11, tm_wday=1, tm_yday=182, tm_isdst=0)
2020/6/30
Tue Jun 30 10:58:11 2020
-
下划线
image.png
- lambda函数
作用同匿名函数,argument_list为函数的参数;expersion为函数体,expression是一个关于参数的表达式,表达式中出现的参数需要在argument_list中有定义,并且表达式只能是单行的。
语法:lambda argument_list:expersion
- map函数
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
print map(sq,[y for y in range(10)])
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]