File input and output (or File I/O) is the ability to read data from and write data to files stored in a location such as a directory or a folder. The ability to handle files is actually a pretty deep subject that requires some measure of interaction with the computer operating system. Fortunately, for high-level languages such as Python, the nuts and bolts of file I/O are absorbed into a relatively simple set of methods. There are three major steps to referencing a file:
- Open the file:
This lets the operating system know the name and location of the file being referenced and how the file is to be used (such as read or write)
- Perform operations on the file data (such as read, write, or append):
Now that the operating system has opened the file, it is ready to be used for the purpose specified in step 1
- Close the file:
After the desired set of operations has been completed, the operating system must be informed that access to the file is no longer necessary.
Here is an example of the three steps you can try in Repl.it:
fhandle = open('examp.txt','w')
fhandle.write('This is a write example. ')
fhandle.write('Text will be sequentially written until a newline control character occurs. \n')
fhandle.write('Then a new line will begin with \n')
fhandle.write('and another new line, etc \n')
The syntax for implementing the above step is fairly straightforward:
- The "open" command creates and opens a file "examp.txt" where "w" means that data will be written to the file. If we were to read data, we would use an "r" instead (the "r" is actually optional where, if omitted, a file read will be assumed). In short,
the first argument to the "open" command is the file name, and the second argument indicates the operation to be performed. An object (named "fhandle" in this example) is created that allows access to a host of methods that will be practiced in this unit.
In Repl.it, the file location will be in the leftmost column under the "main.py" reference. Since Repl.it is web-based, this column effectively acts like your local directory from which files can be downloaded or uploaded.
You should notice that when the "open" command executes, the file "examp.txt" is created. The file is still empty, but it is now ready to be written to.
- Since the file was opened with the parameter "w", data will be written to the file. This operation can be accomplished using the "write" method.
- Finally, the "close" method closes the file. After writing the data to the file, you should be able to click on the filename in the left window and see the text that was written in step 2.
Try adding the following code to the above script:
f2 = open('examp.txt','r')
print(f2.read()) #the 'read' method reads the file
You now have the ability to create a file, as well as read data from and write data to a file. The files source.txt, source2.txt, and source3.txt are provided here. You should upload them into the leftmost window in Repl.it for your code to reference them for a file read. You can then practice the examples on this page.