how to write and read file in python code example
Example 1: python write to file
file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
Example 2: read file python
document = 'document.txt'
file = open(document, 'r')
file.seek(0)
for i in file:
k = i.strip()
print k
file.close()
with open(document) as ur:
for i in ur:
k = i.strip()
print k
Example 3: how to save to file in python
with open('data.txt', 'w') as my_data_file:
my_data_file.write(WhateverYourInputIs)