Py.1.0
import time
python是这样的
time1
2
3
4
5
6
7
8
9
10
11import time
time.time() #获取当前时间戳(从1970年起的秒数)。
time.sleep(secs) #暂停程序执行指定秒数。
time.localtime([secs]) #将时间戳转为本地时间的 struct_time 对象。如果不传参数,使用当前时间。
time.gmtime([secs]) #将时间戳转为 UTC 时间的 struct_time 对象。如果不传参数,使用当前时间。
time.strftime(fmt, t) #将时间对象(struct_time)格式化为字符串,fmt 为格式化规则。
time.strptime(str, fmt)# 将字符串时间解析为 struct_time 对象,fmt 为格式化规则。
time.asctime([t]) #将 struct_time 对象转为可读字符串。如果不传参数,使用当前时间。
time.ctime([secs]) #将时间戳直接转为可读字符串。如果不传参数,使用当前时间。
time.mktime(t) #将 struct_time 对象转为时间戳(秒数)。
time.perf_counter() #获取高精度计时器的值,适合测量程序运行时间。1
file = open(filename, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
file = open(“example.txt”, “w”)
file.write(“Hello, World!”)
file.close()
‘r’:只读模式(默认)。文件必须存在,否则会抛出 FileNotFoundError。
file = open(“example.txt”, “r”)1
2
3
4
5
6~~~
'w':写入模式。如果文件存在,会覆盖文件内容;如果文件不存在,会创建新文件。
file = open("example.txt", "w")
file.write("Hello, World!")
file.close()1
2
3
4'a':追加模式。如果文件存在,数据会被添加到文件的末尾;如果文件不存在,会创建新文件。
file = open("example.txt", "a")
file.write("Append this line.\n")
file.close()1
'x':独占创建模式。如果文件已存在,会抛出 FileExistsError。
1
2
3
4
5
6file = open("example.txt", "x")
'b':二进制模式。用于读取或写入二进制文件,例如图片、音频等。
file = open("example.jpg", "rb")
content = file.read()
file.close()1
2't':文本模式(默认)。用于读取或写入文本文件(如 .txt)。如果没有指定,默认为 't' 模式。
file = open("example.txt", "rt") # 或者直接 open("example.txt", "r")1
2
3
4
5'r+':读写模式。文件必须存在,文件指针位于文件开头,既可以读取也可以写入。
file = open("example.txt", "r+")
content = file.read()
file.write("Updated content")
file.close()1
2
3
4
5'w+':写读模式。文件不存在时会创建,文件存在时会覆盖文件内容。
file = open("example.txt", "w+")
file.write("New content")
file.close()1
2
3
4'a+':追加读写模式。文件存在时,内容会追加到文件末尾,文件指针也会位于文件末尾。
file = open("example.txt", "a+")
file.write("Append more content\n")
file.close()