文件的写入


1、使用write方法将数据写入到文件中

with open('test.txt', 'w') as file:
    file.write('我爱Python!')

2、使用writelines方法将多个数据写入到文件中

with open('test.txt', 'w') as file:
    file.writelines(['我爱Python!', '我爱Python!'])

3、指定编码将数据写入文件(默认使用系统编码)

with open('test.txt', 'w', encoding='utf-8') as file:
    file.write('我爱Python!')

results matching ""

    No results matching ""