Using text files in Python

file-handling

Learning Objectives


In this post you will learn how to code the main file handling operations to manipulate text files as follows:

  • Opening a file
  • Reading from a text file
  • Reading writing to a text file
  • Appending to a text file
  • Closing a file
Open a fileClose a fileReadWriteAppend to
To open a file use the following code:

Folder name/File name


When opening a text file you need to specify the filename you want to open. If the file is not stored in the same folder as your Python file, you will also need to specify the location (e.g. subfolder) where the text file is saved. E.g.

Access Mode


When opening the file you have to specify the access mode using one of the following codes:

Mode Description
r Opens a file in read only mode. This is the default mode.
r+ Opens a file for both reading and writing.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist yet, it will create the new file for writing.
w+ Opens a file for both writing and reading. Overwrites the file if the file exists. If the file does not exist yet, it will create the new file for writing.
a Opens a file for appending. The file pointer is at the end of the file. So new data will be added at the end of the file. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Once you have finished using your file, make sure that you close this file! It will free up resources and allow other users/processes to access this file if they need to.
Closing a file is extremely easy using the close method.

To read a text file, line by line, you will need to:

  1. Open the text file in read mode (either “r” or “r+”),
  2. Loop through the text file, line by line, using a for loop,
  3. Close the text file.

Reading line by line

What’s About CSV files?


CSV files (Comma Separated Values) are text files that store data where each record is stored on a line and each field is separated using a comma “,”. Sometimes we use other symbols instead of the comma such as a semi-colon “;” or a pipe “|”.
In this case when reading a line of the text file you can use the split() method to then access each field one by one.
To write to a text file you will need to:

  1. Open the text file in read mode: “w”,
  2. Write to the file using the write() method,
  3. Close the text file.

Be careful, when using the write command, you are overwriting the content of your file. If instead of overwriting the content of your file you want to append (write at the end of the file) check the next tab: “Append to a text file”.

Note that when opening the text file, if the file specified does not exist, Python will create a new file automatically.

Writing to a text file

The “\n” at the end of the text means “new line”. Only use it if you want the next call to the write() method to start on a new line.
The write method will overwrite the content of your file. If instead you want to write at the end of a text file you can use the “Append access mode”:

To append to a text file you will need to:

  1. Open the text file in append mode: “a”,
  2. Write to the file using the write() method,
  3. Close the text file.

Appending to a text file

The “\n” at the end of the text means “new line”. Only use it if you want the next call to the write() method to start on a new line.

Practice makes perfect


Try one of these challenges to put into practice what you have learnt about file handling operations.
Class Register(Handling Text Files) My Mp3 Playlist(Handling Text Files)
Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges
One comment on “Using text files in Python
  1. 101CC says:

    If you are lokking for a way to delete a line from a text file you maty find this code quite useful:
    How to delete a specific line from a text file

Our Latest Book