如何使用python读取和写入文件? [英] How to read and write a file using python?

查看:131
本文介绍了如何使用python读取和写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本(从AJAX获取)写入文件,然后读取。

I want to write the text (which I get from AJAX) to a file, and then read it.

推荐答案

以下代码用于从文件中读取内容

The following code for read the content from a file

handle=open('file','r+')
var=handle.read()
print var




如果你想要读取一行使用
readline()。如果你想读取
文件中的整行使用
readlines()也

If you want to read a single line use the readline(). If you want to read the whole lines in the file use the readlines() also

以下将内容写入文件的代码

The following code for writing the content to the file

handle1=open('file.txt','r+')
handle1.write("I AM NEW FILE")
handle1.close()

这篇关于如何使用python读取和写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆