Write() 函数

2021-12-30  本文已影响0人  Co酱_秋乏术

write( )函数的参数是字符串,分以下2种情况:

通过write()函数向文件中写入一行

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'w')

>>> f.write('hello,world!\n') #包含一个换行符。

>>> f.close()

通过write()函数向文件中写入多行

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'w')

>>> f.write('hello python!\nhello world!\n')#包含多个换行符

>>> f.close()

运行程序,结果如下

上一篇 下一篇

猜你喜欢

热点阅读