User Tools

Site Tools


python:file_handling

Python - File handling

Step 1 — Creating a Text File

Before we can begin working in Python, we need to make sure we have a file to work with. To do this, we’ll open up a text editor and create a new txt file, let's call it days.txt.

In the new file, enter a few lines of text. In this example, let's list the days of the week:

days.txt
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Next, save your file and make sure you know where you put it. In our example, our user john, saved the file here: /home/john/days.txt. This will be very important in later steps, where we open the file in Python.

Now that we have a txt file to process, we can begin our code!

Opening a File

Before we can write our program, we have to create a Python programming file, so create the file files.py with your text editor. To make things easy, save it in the same directory as our days.txt file: /home/john/.

To open a file in Python, we first need some way to associate the file on disk with a variable in Python. This process is called opening a file. We begin by telling Python where the file is. The location of your file is often referred to as the file path. In order for Python to open your file, it requires the path. The path to our days.txt file is: /home/john/days.txt. In Python, we will create a string variable to store this information. In our files.py script, we will create the path variable and set the variable to the days.txt path.

files.py
path = '/home/john/days.txt'

We will then use Python's open() function to open our days.txt file. The open() function requires as its first argument the file path. The function also allows for many other parameters. However, most important is the optional mode parameter. Mode is an optional string that specifies the mode in which the file is opened. The mode you choose will depend on what you wish to do with the file. Here are some of our mode options:

  • 'r' : use for reading
  • 'w' : use for writing
  • 'x' : use for creating and writing to a new file
  • 'a' : use for appending to a file
  • 'r+' : use for reading and writing to the same file

In this example, we only want to read from the file, so we will use the 'r' mode. We will use the open() function to open the days.txt file and assign it to the variable days_file.

files.py
days_file = open(path,'r')

After we have opened the file, we can then read from it, which we will do in the next step.

Reading a File

Since our file has been opened, we can now manipulate it (i.e. read from it) through the variable we assigned to it. Python provides three related operations for reading information from a file. We'll show how to use all three operations as examples that you can try out to get an understanding of how they work.

The first operation <file>.read() returns the entire contents of the file as a single string.

files.py
days_file.read()

Returns:

'Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n'

The second operation <file>.readline() returns the next line of the file, returning the text up to and including the next newline character. More simply put, this operation will read a file line-by-line.

files.py
days_file.readline()

Returns:

'Monday\n'

Therefore, once you read a line with the readline operation it will pass to the next line. So if you were to call this operation again, it would return the next line in the file, as shown.

files.py
days_file.readline()

Returns:

'Tuesday\n'

The last operation, <file>.readlines() returns a list of the lines in the file, where each item of the list represents a single line.

files.py
days_file.readlines()

Returns:

['Monday\n', 'Tuesday\n', 'Wednesday\n', 'Thursday\n', 'Friday\n', 'Saturday\n', 'Sunday\n']

Something to keep in mind when you are reading from files, once a file has been read using one of the read operations, it cannot be read again. For example, if you were to first run days_file.read() followed by days_file.readlines() the second operation would return an empty string. Therefore, anytime you wish to read from a file you will have to first open a new file variable. Now that we have read from a file, let's learn how to write to a new file.

Writing a File

In this step, we are going to write a new file that includes the title Days of the Week followed by the days of the week. First, let's create our title variable.

files.py
title = 'Days of the Week\n'

We also need to store the days of the week in a string variable, which we'll call days. To make it easier to follow, we include the code from the steps above. We open the file in read mode, read the file, and store the returned output from the read operation in our new variable days.

files.py
path = '/home/john/days.txt'
days_file = open(path,'r')
days = days_file.read()

Now that we have variables for title and days of the week, we can begin writing to our new file. First, we need to specify the location of the file. Again, we will use the directory /home/john/. We will have to specify the new file we wish to create. So, our path will actually be /home/john/new_days.txt. We provide our location information in the new_path variable. We then open our new file in write mode, using the open() function with the 'w' mode specified.

files.py
new_path = '/home/john/new_days.txt'
new_days = open(new_path,'w')

Important to note, if new_days.txt already existed before opening the file its old contents would have been destroyed, so be careful when using the 'w' mode.

Once our new file is opened, we can put data into the file, using the write operation, <file>.write(). The write operation takes a single parameter, which must be a string, and writes that string to the file. If you want to start a new line in the file, you must explicitly provide the newline character. First, we write the title to the file followed by the days of the week. Let's also add in some print statements of what we are writing out, which is often good practice for tracking your scripts' progress.

files.py
new_days.write(title)
print(title)
 
new_days.write(days)
print(days)

Lastly, whenever we are finished with a file, we need to make sure to close it. We show this in our final step.

Closing a File

Closing a file makes sure that the connection between the file on disk and the file variable is finished. Closing files also ensures that other programs are able to access them and keeps your data safe. So, always make sure to close your files. Now, let's close all our files using the <file>.close() function.

files.py
days_file.close()
new_days.close()

We're now finished processing files in Python and can move on to looking over our code.

Checking our Code

Before we run our code, let's make sure everything looks good. The final product should look something like this:

files.py
path = '/home/john/days.txt'
days_file = open(path,'r')
days = days_file.read()
 
new_path = '/home/john/new_days.txt'
new_days = open(new_path,'w')
 
title = 'Days of the Week\n'
new_days.write(title)
print(title)
 
new_days.write(days)
print(days)
 
days_file.close()
new_days.close()

After saving your code, open up terminal and run your Python script, like so:

python files.py

Our output should look like this:

Days of the Week

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Now, let's double check our code fully worked by opening our new file (new_days.txt). If all went well, when we open our new file, it should look like this:

new_days.txt
Days of the Week
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Your file will look the same or similarly — you have successfully completed this tutorial!

python/file_handling.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki