Answers for "python store data in file"

30

python make txt file

file = open("text.txt", "w") 
file.write("Your text goes here") 
file.close() 
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
Posted by: Guest on April-04-2020
22

python append to file

with open(filename, "a+") as f:
  f.write('Hello World')
Posted by: Guest on March-30-2020
1

python store data in file

import pickle
file_name = 'data_stuff'
try:
    # this will create the file if it doesnt already exist
    history_data = open(file_name + ".dat", "x")
    history_data.close()
    history_data = []
    pickle.dump(history_data, open(file_name + ".dat", "wb"))
except:
    # if the file already exist it will load the history_data array 
    # you can add to it or modity it or just read it.
    history_data = pickle.load(open(file_name + ".dat", "rb"))

print(history_data) #this will print the history_data array that was stored in the file
foo = 5
history_data.append(foo)
print(history_data)
pickle.dump(history_data, open(file_name + ".dat", "wb")) # this saves the newly modified history_data to the file
Posted by: Guest on August-14-2021
7

read and write to file python

file = open(“testfile.txt”, “r+”) 
Posted by: Guest on May-24-2020

Code answers related to "python store data in file"

Python Answers by Framework

Browse Popular Code Answers by Language