速战速决 Python - python 基础: 文件写入和读取

速战速决 Python https://github.com/webabcd/PythonSample
作者 webabcd

速战速决 Python - python 基础: 文件写入和读取

示例如下:

basic/file.py

# 文件写入和读取

# 文件路径
path = r'd:\temp.txt'

# 写文件
fw = open(path, 'w')
fw.write('hello webabcd')
fw.close()

# 读文件
fr = open(path, 'r')
print(fr.read())
fr.close()

# with 语句,会在代码块执行完后自动调用 close()
# 写文件
with open(path, 'w', encoding='utf-8') as fw2:
    fw2.write('hello webabcd2')
    # 执行完后会自动调用 fw2.close()

# with 语句,会在代码块执行完后自动调用 close()
# 读文件
with open(path, 'r', encoding='utf-8') as fr2:
    print(fr2.read())
    # 执行完后会自动调用 fr2.close()

速战速决 Python https://github.com/webabcd/PythonSample
作者 webabcd

posted @ 2022-01-20 16:11  webabcd  阅读(40)  评论(0编辑  收藏  举报