小节将会介绍os,sys,array,math,time库中得常用函数使用
该模块包含文件系统访问和urandom的功能。
函数说明:查看系统信息。 示例:
>>> import os
>>> os.uname()
(sysname='esp8266', nodename='esp8266', release='2.2.0-dev(9422289)', version='v1.11-8-g48dcbbe60 on 2019-05-29', machine='ESP module with ESP8266')
函数说明:没有参数时显示当前目录,给定参数时显示给定的目录。 示例:
1
2
3
4
>>> os.listdir()
['.history', '.vscode', 'MD', '.mpyproject.json', 'main_example.py', 'dir']
>>> os.listdir("./lib")
['test.py']
函数说明:创建目录,path为创建目录的路径。 示例:
>>> os.listdir()
['boot.py']
>>> path = "./lib"
>>> os.mkdir(path)
>>> os.listdir()
['boot.py', 'lib']
函数说明:删除目录。 示例:
>>> os.listdir()
['boot.py', 'lib']
>>> os.rmdir("./lib")
>>> os.listdir()
['boot.py']
函数说明:删除文件。 示例:
>>> os.listdir("./lib")
['test.py']
>>> os.remove("./lib/test.py")
>>> os.listdir("./lib")
[]
函数说明:查看当前目录。 示例:
>>> os.getcwd()
'/'
函数说明:更改当前目录。 示例:
>>> os.getcwd()
'/'
>>> os.chdir("./lib")
>>> os.getcwd()
'/lib'
>>> os.chdir("..")
>>> os.getcwd()
'/'
函数说明:重命名文件。 示例:
>>> os.listdir(os.getcwd())
['test.py']
>>> os.rename("test.py", "mytest.py")
>>> os.listdir(os.getcwd())
['mytest.py']
函数说明:获取文件或目录的状态。 示例:
>>> os.stat("./lib")
(16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
>>> os.stat("./lib/test.py")
(32768, 0, 0, 0, 0, 0, 1, 0, 0, 0)
函数说明:用n个随机字节返回一个字节对象。 示例:
>>> os.urandom(20)
b'f\x92\x85t28\xa1\xf0\xaf3\xf5\xd9\xcdx\xc3\n\xedm
sys模块中提供了与MicroPython运行环境有关的函数和变量。
>>> import sys
>>> print(sys.path)
['', '/lib']
>>> print(sys.version)
3.4.0
1
2
>>> print(sys.implementation)
(name='micropython', version=(1, 11, 0))
对于 MicroPython,它返回下面属性:
名称 - “micropython”
版本 - (主, 次, 微), 如 (1, 7, 0)
这个方法推荐用来识别不同平台的MicroPython。
>>> sys.platform
'esp8266'
1
2
>>> print(sys.byteorder)
little
“little”表示小端存储,“big”表示大端存储。
>>> print(sys.maxsize)
2147483647
sys模块中提供了绑定在系统路径上的函数,通过help()可以查看,这里不作介绍。 示例:
>>> help(sys.path)
object ['', '/lib'] is of type list
append -- <function>
clear -- <function>
copy -- <function>
count -- <function>
extend -- <function>
index -- <function>
insert -- <function>
pop -- <function>
remove -- <function>
reverse -- <function>
sort -- <function>
>>> sys.path.append('test')
>>> sys.path
['', '/lib', 'test']
函数说明:退出当前程序。 示例:
>>> i = 0
>>> while True:
... i = i+1
... print(i)
... if(i == 6):
... sys.exit()
...
...
...
1
2
3
4
5
6
类说明:用给定类型的元素创建数组。数组的初始内容由iterable提供,如果没有提供,则创建一个空数组。
typecode:数组的类型
iterable:数组初始内容
示例:
>>> import array
>>> a = array.array('i', [2, 4, 1, 5])
>>> b = array.array('f')
>>> print(a)
array('i', [2, 4, 1, 5])
>>> print(b)
array('f')
函数说明:将一个新元素追加到数组的末尾。 示例:
>>> a = array.array('f', [3, 6])
>>> print(a)
array('f', [3.0, 6.0])
>>> a.append(7.0)
>>> print(a)
array('f', [3.0, 6.0, 7.0])
函数说明:将一个新的数组追加到数组的末尾,注意追加的数组和原来数组的数据类型要保持一致。 示例:
>>> a = array.array('i', [1, 2, 3])
>>> b = array.array('i', [4, 5])
>>> a.extend(b)
>>> print(a)
array('i', [1, 2, 3, 4, 5])
在math模块中提供了一些使用浮点数的基本数学函数。
math.e — 自然对数的底数。 示例:
>>> import math
>>> print(math.e)
2.718282
math.pi — 圆周长与直径的比值。 示例:
>>> print(math.pi)
3.141593
函数说明:计算平方根。 示例:
>>> x = math.sqrt(9)
>>> print(x)
3.0
函数说明:计算x的y次方(幂)。 示例:
>>> x = math.pow(2, 3)
>>> print(x)
8.0
函数说明:计算e的x次方(幂)。 示例:
>>> x = math.exp(2)
>>> print(x)
7.389056
函数说明:计算math.exp(x)-1。 示例:
>>> math.expm1(2)
6.389056
函数说明:计算以e为底的x的对数。 示例:
>>> x = math.log(10)
>>> print(x)
2.302585
函数说明:计算以2为底的x的对数。 示例:
>>> x = math.log2(8)
>>> print(x)
3.0
函数说明:计算以10为底的x的对数。 示例:
>>> x = math.log10(10)
>>> print(x)
1.0
函数说明:角度转化为弧度。 示例:
>>> x = math.radians(60)
>>> print(x)
1.047198
函数说明:弧度转化为角度。 示例:
>>> x = math.degrees(1.047198)
>>> print(x)
60.00002
函数说明:传入弧度值,计算正弦。 示例:计算sin90°
>>> math.sin(math.radians(90))
1.0
函数说明:传入弧度值,计算余弦。 示例:计算cos60°
>>> math.cos(math.radians(60))
0.5
函数说明:传入弧度值,计算正切。 示例:计算tan60°
>>> math.tan(math.radians(60))
1.732051
函数说明:传入弧度值,计算sin(x)的反三角函数。 示例:
>>> x = math.asin(0.5)
>>> print(x)
0.5235988
函数说明:传入弧度值,计算cos(x)的反三角函数。
函数说明:传入弧度值,计算tan(x)的反三角函数。
函数说明:向上取整。 示例:
>>> x = math.ceil(5.6454)
>>> print(x)
6
函数说明:向下取整。 示例:
>>> x = math.floor(2.99)
>>> print(x)
2
>>> y = math.floor(-2.34)
>>> print(y)
-3
函数说明:计算绝对值。 示例:
>>> x = math.fabs(-5)
>>> print(x)
5.0
>>> y = math.fabs(5.0)
>>> print(y)
5.0
函数说明:取x除以y的模。 示例:
>>> x = math.fmod(4, 5)
>>> print(x)
4.0
函数说明:取整。 示例:
>>> x = math.trunc(5.12)
>>> print(x)
5
>>> y = math.trunc(-6.8)
>>> print(y)
-6
函数说明:返回伽马函数。 示例:
>>> x = math.gamma(5.21)
>>> print(x)
33.08715
函数说明:返回伽马函数的自然对数。 示例:
>>> x = math.lgamma(5.21)
>>> print(x)
3.499145
time库用来获取时间和日期、测量时间间隔、延时时间等。
函数说明:睡眠给定的秒数。
sec:睡眠时间
可以为浮点数,整数
示例:
>>> time.sleep(3)
>>> def mySleep():
... time.sleep(3)
... print("hello")
...
>>> mySleep()
hello
执行mySleep()函数后,你会发现过了三秒才会打印“hello”。 其他类似time(sec)的函数: time.sleep_ms(ms):睡眠给定的毫秒数,为int型。 time.sleep_us(us):睡眠给定的微秒数,为int型。
函数说明:获取当前cpu时间戳,单位:秒。 示例:
>>> import time
>>> print(time.time())
21690
函数说明:返回不断递增的毫秒计数器,在某些值后会重新计数。计数毫无意义,除非在ticks.diff()中。 示例:
>>> print(time.ticks_ms())
24612257
其他类似ticks_ms()的函数: time.ticks_us():返回微秒。 time.ticks_cpu():相比前面的函数具有更高精度(返回CPU时钟)。
函数说明:经过偏移时间后的时间戳。
ticks:
ticks_ms()、ticks_us()、ticks_cpu()
delta:任意整数或表达式(注意:必须使用tick_diff功能来处理截止日期)
示例:
>>> def ticks_addTest():
... print(time.ticks_ms())
... time.sleep(2)
... print(time.ticks_add(time.ticks_ms(), -2))
...
>>> ticks_addTest()
23064376
23066374
函数说明:计算两次调用 ticks_ms(), ticks_us(), 或 ticks_cpu()之间的时间。
old_t:开始时间。
new_t:结束时间。
示例:
>>> def ticks_diffTest():
... t1 = time.ticks_ms()
... time.sleep(2)
... t2 = time.ticks_ms()
... t = time.ticks_diff(t1, t2)
... print(t)
...
>>> ticks_diffTest()
-2000