本文共 932 字,大约阅读时间需要 3 分钟。
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() # print the read data print("\nThe Content of File is:") print(str) # close the file fo.close()if __name__=="__main__":main()
Output
输出量
Enter Some Data to Save: Hello friends, how are you?The Content of File is:Hello friends, how are you?
翻译自:
转载地址:http://xzxzd.baihongyu.com/