python文件的写入
来源:互联网 发布:数控车g73编程实例详解 编辑:程序博客网 时间:2024/06/06 13:10
wirte()方法把字符串写入文件,writelines()方法可以把列表中存储的内容写入文件。
f=open("hello.txt","w+",,encoding='utf-8')
li=["hello world\n","hello china\n"]
f.writelines(li)
f.close()
文件的内容:
hello world
hello china
write()和writelines()这两个方法在写入前会清除文件中原有的内容,再重新写入新的内容,相当于“覆盖”的方法。如果需要保留文件中原有的内容,只是需要追加新的内容,可以使用“a+”模式
打开文件。
f=open("hello.txt","a+")
new_context="goodbye"
f.write(new_content)
f.close()
此时hello.txt中的内容如下所示:
hello world
hello china
goodbye
实践:
>>> f=open("hello.txt","w+")
>>> li=["hello world\n","hello china\n"]
>>> f.writelines(li)
>>> f.close()
>>>
>>> f=open("hello.txt","a+")
>>> new_context="goodbye"
>>> f.write(new_content)
>>> f.write(new_content)
>>> f.close()
结果:
hello world
hello china
goodbyegoodbye
0 0
- python文件的写入
- python文件的写入
- python写入xml文件的方法
- python对文件的指定行写入
- 关于python文件的读取创建写入
- Python 读取写入 json 格式的文件
- 我的python学习笔记、写入文件
- python文件的写入write()
- python 文件的读取和写入
- python 写入文件
- python csv文件写入
- Python 写入XML 文件
- python读取写入文件
- python 写入文件
- python换行写入文件
- Python读取写入文件
- python 写入csv文件
- Python 写入CSV文件
- note:linux opencv
- 1014. Waiting in Line (30)
- Palindrome Number
- 在宽度确定的情况下高度自适应
- Windows+Anaconda2&3+Sublime text
- python文件的写入
- 性能测试利器iperf
- wps for Linux
- 工作了
- [LeetCode] Max Sum of Rectangle No Larger Than K
- Java多线程
- Python返回数组(List)长度的方法
- Java经典算法(三)
- Gradle的大型项目常用库和版本管理