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
file = open("myTextFile.txt","r")
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.
file = open("myFolder/myTextFile.txt","r")
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. |
Closing a file is extremely easy using the close method.
file.close()
- Open the text file in read mode (either “r” or “r+”),
- Loop through the text file, line by line, using a for loop,
- Close the text file.
Reading line by line
file = open("myTextFile.txt","r") for line in file: print(line) file.close()
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.
file = open("myTextFile.txt","r") for line in file: data = line.split(";") print(data[0] + " - " + data[1] + " - " + data[2]) file.close()
- Open the text file in read mode: “w”,
- Write to the file using the write() method,
- 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
file = open("myTextFile.txt","w") file.write("Hello World\n"); file.close()
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.
To append to a text file you will need to:
- Open the text file in append mode: “a”,
- Write to the file using the write() method,
- Close the text file.
Appending to a text file
file = open("myTextFile.txt","a") file.write("Hello World\n"); file.close()
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)