Python | 从用户输入数据,保存到文件,读取并打印

这篇博客介绍了如何使用Python的input()、write()和read()方法从用户接收数据,将其保存到文件,并再次读取及打印出来。提供了一段实现此功能的Python代码。
摘要由CSDN通过智能技术生成

Here, we have to input the data from the user, to read the data from user, we use input() method, and then the input data we have to store in the file by using write() method and then by using read() method we can get the data.

在这里,我们必须从用户输入数据,从用户读取数据,我们使用input()方法,然后我们必须使用write()方法然后使用read( )将输入数据存储在文件中)方法即可获取数据。

Here is the python code to save and read the data:

这是保存和读取数据的python代码:

def main():
	# open file in write mode
	fo = open("data2.txt","wt")
	# Input the user data
	data = input("Enter Some Data to Save: ")
	# write data to the file
	fo.write(data)
	# close the file
	fo.close()

	# open file again in read mode
	fo = open("data2.txt","rt")
	# read the data from the file
	str=fo.read()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值