Tutorials on Advanced Math and Computer Science Concepts

File IO in Python

Programming centers around taking inputs from users, and computing results based on them. The way that a user provides this input does not always need to be limited to typing the input in. Often, it is valuable to be able to read a file a user provides, or possibly even write results to a file as well. A good example of reading a file is creating a configuration file. Often, we want the user to be able to customize certain aspects of the application. For cases like these, a configuration file is often convenient since the user can easily access and update it as required.  Writing to files is often used to save the output of a program to a place where it can be easily shared with others. Often this will be in the format of a CSV so it can be imported to databases or Excel.

For example, suppose you wanted to open a file provided by the user, and print the contents of the file onto the screen. A task like this is commonplace in programs like Word, where a user can open a document and the content is displayed. To do this in Python, you can use the following code.

First, we ask the user to input the file path of the file they wish to open. Next, we open the file using the function open(path,"r"). The open function takes in two arguments, the path of the file that is to be opened, and the mode to use the file in. "r" means that the file will be used in read mode, meaning you can read from the file, but not write to it. The following attributes exist for opening files:

  1. "r": This will open the file in read mode, meaning it can be read but not written to. It will return an error if the file targeted does not exist.
  2. "w": This will open the file in write mode, meaning it can be written to but not read. It will create the file targeted if it does not already exist.
  3. "a": This will open the file in append mode, meaning it can write to the end of the file, but not read. The difference between append and write mode is that append will write to the end of the file, whereas write will overwrite the current file completely. It will create the file targeted if it does not already exist.
  4. "x": This will open the file in create mode, meaning it will create the file if it doesn't already exist. If the file does exist, this will return an error.  

From here, we can call the read method, which will read the whole file.

Using the modes, we can open and manipulate files in a variety of different ways. As another example, suppose we wanted to ask a user to input a value, and we wanted to write that value to a new file. To do this, we can open a new file in write mode, and write the desired values using the write method.

Keep in mind that unlike append, write is going to completely remove all content that exists in the file, and then write what we ask it to write. If we want to preserve what is currently in the file, we need to use append instead.

Overall, the basic file operations in Python are simple but powerful. Often accessing files to read and write is an important aspect of our programs, so understanding these methods is essential.