path = 'report'
text ='pass'
file = open(r"E:\{}\report.txt".format(path), mode="a+")
file.writelines("{}\n".format(text))
file.close()
运行结果:
D:\Python3.8.6\python.exe D:/PythonWorkSpace/chenbang/test_5.py
Process finished with exit code 0
运行截图:
注意事项:
每次写入后及时关闭文件(file.close()),避免后面要继续写入时文件冲突导致报错