write data to text file python code example
Example 1: python read file to variable
with open('data.txt', 'r') as file:
data = file.read().replace('\n', '')
Example 2: python writing to text file
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
f = open("demofile2.txt", "r")
print(f.read())
Example 3: 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 4: save python output to text file
$ python my_program.py > output.txt
Example 5: how to make a text file in python
f= open("guru99.txt","w+")