It is often useful to have programs read and write data to/from files, particularly when there are more than a few input values a program needs or the program produces a lot of results that a user may want to save.
A file is a sequence of characters. However, unlike a list or a string, characters in the file must be read or written from the file in order starting from the first one. The current position in the file is the next location where data will be read or written to in a file. When a file is first opened, the current position is at the very first character in the file. Reading or writing data from/to a file changes the current position. For example, if the file contents are:
Hello There How are you?A first read of 7 chars will return the string "Hello T", a subsequent read of 8 chars will return the string "here\nHow" (all characters, including whitespace characters like spaces, tabs, and '\n', count).
infile = open("inDat.txt", "r") # open file named inDat.txt for reading outfile = open("outDat.txt", "w") # open file named outDat.txt to write
s = infile.readline() # read first line of file (up to and including '\n') outfile.write(s) # write s to the very beginning of outfileThe next read or write picks up where the previous one left off:
s = infile.readline() # read the second line of file outfile.write(s) # write s at the point after the first write left offAt the end of the file is a special end of file character (EOF). In Python, calls to read method functions will return an empty string when they reach the end of the file:
# read each line of a file and print to terminal s = infile.readline() while(s != ""): print(s) s = infile.readline() # read the next line
infile.close() outfile.close()
s = infile.read(3)
s = infile.readline()
l = infile.readlines() print(l[0]) # prints out the first line of the file
infile = open("textfile.txt", "r") lines = [] for line in infile: lines.append(line) # process each line (here: add it to list of lines) infile.close()
$ python >>> outfile = open("poem.txt", "w") >>> outfile.write("Roses are red.\n") >>> outfile.write("Violets are blue.\n") >>> outfile.write("Sugar is sweet,\n") >>> outfile.write("And so are you!\n") >>> outfile.close() >>> $ cat poem.txt Roses are red. Violets are blue. Sugar is sweet, And so are you!
>>> infile = open("poem.txt","r") >>> lines = infile.readlines() >>> infile.close() >>> print(lines) ['Roses are red.\n', 'Violets are blue.\n', 'Sugar is sweet,\n', 'And so are you!\n']
>>> infile = open("poem.txt","r") >>> lines = [] >>> for line in infile: ... lines.append(line.strip()) ... >>> infile.close() >>> print(lines) ['Roses are red.', 'Violets are blue.', 'Sugar is sweet,', 'And so are you!']
infile = open("poem.txt","r") lines = [] s = infile.readline() while(s != ""): lines.append(s.strip()) s = infile.readline() # read the next line infile.close() print(lines) ['Roses are red.', 'Violets are blue.', 'Sugar is sweet,', 'And so are you!']This while loop is doing the exact same thing as the for loop example, just in a different way.
In that last two examples, using the for loop or using the while loop, we are able to process each line as we read it in. The str strip() method is used to strip off leading and trailing whitespace (the '\n' at the end of each line). Then the stripped line is appended to the list. The end result is similar to using readlines().
For more info on string and list methods, see our str and list methods page.