简体   繁体   中英

Store and recall a variable in a text file Python

How would i store the variable x on a specific line (the first)in a text file and recover it as the program opens.

Also how would i store data in a text file on a specific line

You should take a look at the open() built-in.

#To write to a file:
with open("file.txt", "w") as f:
    f.write("Data!")

#To read from a file:
with open("file.txt", "r") as f:
    print(f.read())

Next time, you should check for duplicates:

easy save/load of data in python , Writing a Top Score to a data file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM