Python:如何使用writelines()函数写文件

2024-10-13 08:29:10

1、打开PyCharm软件,创建一个Python文件。

Python:如何使用writelines()函数写文件

2、以追加模式打开目标文件。

Python:如何使用writelines()函数写文件

3、定义要增加的内容:可以是字符串,也可以是列表。

Python:如何使用writelines()函数写文件

4、用writelines()函数将定义的内容写入文件中,然后关闭文件。

Python:如何使用writelines()函数写文件

5、运行Python文件后,源文件增加了第三步中writelines()函数指定的内容。

Python:如何使用writelines()函数写文件

6、验证writelines()函数是否有返回值。执行Python文件后会报错,表示该函数没有返回值。

Python:如何使用writelines()函数写文件Python:如何使用writelines()函数写文件
猜你喜欢