1with open("file.txt") as file_in:
2 lines = []
3 for line in file_in:
4 lines.append(line)
1file = open("text.txt", "w")
2file.write("Your text goes here")
3file.close()
4'r' open for reading (default)
5'w' open for writing, truncating the file first
6'x' open for exclusive creation, failing if the file already exists
7'a' open for writing, appending to the end of the file if it exists
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# How to read, and print to the screen a file in python!
2
3f = open('fileName', 'r')
4print(f.read())
5f.close()
6
7# Where "fileName" is obviously the name of your file that you want to read.
1# when you want to read the file from terminal in python use
2import sys
3f = open(sys.argv[1])
4line=f.readline()