下面是一个简单的Python代码,用于将文本写入文件中:

# 打开文件,如果不存在则创建
with open('example.txt', 'w') as file:
    # 将文本内容写入文件
    file.write('Hello, world!\n')
    file.write('This is an example file.\n')
    file.write('Python is awesome.\n')

# 打开文件,读取并输出文本内容
with open('example.txt', 'r') as file:
    contents = file.read()
    print(contents)

输出结果:

Hello, world!
This is an example file.
Python is awesome.
编写文件写入代码用python代码格式输出

原文地址: https://www.cveoy.top/t/topic/bY7Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录