Python | 从用户输入数据,保存到文件,读取并打印
生活随笔
收集整理的這篇文章主要介紹了
Python | 从用户输入数据,保存到文件,读取并打印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 modefo = open("data2.txt","wt")# Input the user datadata = input("Enter Some Data to Save: ")# write data to the filefo.write(data)# close the filefo.close()# open file again in read modefo = open("data2.txt","rt")# read the data from the filestr=fo.read()# print the read dataprint("\nThe Content of File is:")print(str)# close the filefo.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?翻譯自: https://www.includehelp.com/python/input-data-from-the-user-save-to-the-file-read-and-print.aspx
總結
以上是生活随笔為你收集整理的Python | 从用户输入数据,保存到文件,读取并打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二进制 |_元二进制搜索| 单边二元搜
- 下一篇: map函数python求数的每位的值_m