1file = open(“testfile.txt”,”w”)
2
3file.write(“Hello World”)
4file.write(“This is our new text file”)
5file.write(“and this is another line.”)
6file.write(“Why? Because we can.”)
7
8file.close()
1#there are many modes you can open files in. r means read.
2file = open('C:\Users\yourname\files\file.txt','r')
3text = file.read()
4
5#you can write a string to it, too!
6file = open('C:\Users\yourname\files\file.txt','w')
7file.write('This is a typical string')
8
9#don't forget to close it afterwards!
10file.close()
1fin = open("NAME.txt", 'r')
2body = fin.read().split("\n")
3line = fin.readline().strip()
1f = open("demofile3.txt", "w")
2f.write("Woops! I have deleted the content!")
3f.close()
4
5#open and read the file after the appending:
6f = open("demofile3.txt", "r")
7print(f.read())