Python Write To Text File And Read From File Tutorial!
There are many instances that writing data to or reading data from a local file is easier than to a database. Our Python write to text file and read from file tutorial will show you the easy way to create, write, and read to a local file to store program-related data.
We will use Python’s built-in open()
function to get a file object.
Python’s open()
function returns a file object.
File Objects have methods for writing and reading files. File objects are great to create, modify, and read from files.
In addition to methods, the File Objects have attributes as well. A File object’s attribute relate to what the Object file is pointing to, whether the file is open or closed, and more file related information.
Python Write to Text File: Syntax
Our open()
function has the following syntax.
file_object = open('filename_to_open','mode_to_open_file')
- file_object – is the variable place holder for the File Object returned by the open function
- = – assigns the File Object returned to the variable place holder
- open(‘filename_to_open’,’mode_to_open_file’) – is the function call to retrieve a File Object
The command above can be understood as open our filename_to_open
with the mode_to_open_file
specified and save it to file_object
Python Write to Text File: Modes
We have been saying the mode to open a File Object, this can be noted as the mode_to_open_file
in our syntax section above.
The modes available to the open()
function are as follows:
- ‘r’ – Read mode. (Default if not specified)
- Read mode allows the File object to read and pull data out of the file
- ‘w’ – Write mode.
- Write mode allows the File object to write to a file. It is important to note that using the write mode will delete the file contents if you specify a file that exists
- ‘a’ – Append mode.
- Append mode is similar to write mode but will not delete a file’s contents if it exists it will simply begin writing at the end of the file. If the file does not exist then it will create it and act like write mode.
- ‘+’ – Read and Write mode.
- Read and Write mode allows both reading and writing to happen using the same file object.
- ‘x’ – Exclusive Creation mode.
- Open the file and create it only if it does not exist. It is important to note this will fail to open if the file exists
- ‘b’ – Binary mode.
- Switches from assuming a file are text to assuming it is binary
- ‘t’ – Text mode. (Default if not specified)
- File object will operate assuming the file is a text file.
Python Write to Text File: File Types
It is important to understand what a file looks like to Python and to the underlying Operating System. It is also important to understand the structure of the file to be able to successfully write to and read from files. Most users are used to text files, images, executables, and other file formats that they may have encountered. Python recognizes as files being either text or binary. The difference is how the data is interpreted and encoded.
Note: a text file is only text data but a binary can contain both text and custom binary data.
A text file’s data is interpreted as characters, but a binary file’s data is custom binary data.
Text File Details
Text files are sequences of characters that are terminated by an End of Line, EOL, character. The most common End of Line character is a newline character.
A new Line character is known as ‘\n’. Although it’s perceived as two characters by users the computer understands it as a single character. The backslash means to escape the character’s normal meaning. Other special characters that use the ‘\’ to escape them are as follows:
- New Line – ‘\n’
- Carriage return – ‘\r’
- Important note: a carriage return is used in conjunction with newlines to make Windows EOL. So Window’s EOL looks like this ‘\r\n’ and is usually called CRLF or Carriage return and Line feed. Linux and Mac generally only use the ‘\n’
- Tab space – ‘\t’
Common extensions that are considered text files:
- Web : html, css, xml, json
- Documents: txt, rtf
- Configuration: ini, cfg
- Source code: c, cs, js, py, java, php, sh, pl
Binary File Details
It is important to note that manipulating binary files can result in corrupting the file much faster than messing with text files. A binary file is any file that has custom binary data that is generally only read and interpreted by a specific program.
Common extensions that are considered binary files:
- Images: gif, jpg, png
- Videos: mpeg, mpg, mp4, mov
- Documents: pdf, doc, docx, ppt, pptx
- Archival: zip, 7z, iso
- Executable: exe, dll
Python Write to Text File: Write Example
Although describing and showing the syntax is nice it’s always important to get hands on. We will have Python write to a text file that does not exist.
Note: Python 3 must be installed and you must have access to a simple editor to do the following examples and exercises.
Simple Write Example
We are going to create a file named ‘hello.txt’ and insert our name and a greeting. You can name yours anything you want but for this example, we are setting up a simple creation and writing exercise. You can enter the following lines in your own hello.py file for this exercise.
hello_file = open('hello.txt','w')
hello_file.write('Hi ')
hello_file.write('My name is Name ')
hello_file.write('Nice to meet you')
hello_file.close()
When you run the simple python program it will create a ‘hello.txt’ file in the same location your hello.py file is located.
When we open the file we find the following text inside:
Hi My name is Lauro Nice to meet you
Congratulations you’ve just written your file to disk!
Context Manager
Although you may find several instances of python programs using the open and then later closing the file there is an improved way to do this. Relying on closing the file later in the program can result in unclosed file accessing objects.
Leaving file objects open can result in a program leaking file descriptors and increases the possibility of a program error due to no available file descriptors.
File Descriptors or File Handles (Linux vs Windows) are the way the operating system handle file manipulation, access, and interface. The descriptors or handlers are finite in the amount available and there are multiple programs opening files at any time in a busy computer environment.
The improved way is by using the with
statement. The with
statement is a context manager. Context managers allocate and release system resources when they are being used and leave less room to forget to release a resource.
Improved Write Example
We are going to create a file named ‘improvedHello.txt’ and write to a file that we don’t need to use the close()
function. You can name yours anything you want. You can enter the following lines in your own improvedHello.py file for this exercise.
with open('improvedHello.txt','w') as improved_hello:
improved_hello.write('Hi ')
improved_hello.write('This program does not need ')
improved_hello.write('to use the close() function!')
When we open the ‘improvedHello.txt’ file we find the following text inside:
Hi This program does not need to use the close() function!
Congratulations you’ve just written your file to disk using the improved method!
Python Write to Text File: Read Example
Simple Read Example – One line at a time
We are going to read from the simple file we created. You can go back and enter more text and make it however you want. For our example we have modified our hello.txt to the following:
Hi My Name is Name Welcome to my simple program!
You can enter the following lines in your own read.py file for this exercise.
simple_read = open('hello.txt')
first_line = simple_read.readline()
print(first_line)
simple_read.close()
When you run the simpleRead.py python program it will read in only the first line in the ‘hello.txt’ file and then print it out using a normal print command.
Your normal output should display
> Hi My Name is Name
Congratulations you’ve just read a line from your file!
Simple Read Example – All lines at one time
We are going to continue to use the file we created and modified.
You can enter the following lines in your own allRead.py file for this exercise.
simple_read = open('hello.txt')
all_lines = simple_read.readlines()
print(all_lines)
simple_read.close()
When you run the allRead python program it will read in all the lines in the ‘hello.txt’ file and then print it out using a normal print command.
Your normal output should display
$ ['Hi My Name is Name\n', 'Welcome to my simple program!']
If you want to access a specific line we can modify our allRead.py you would use normal array accessing notation.
simple_read = open('hello.txt')
all_lines = simple_read.readlines()
print('first line =', all_lines[0])
print('second line =', all_lines[1])
simple_read.close()
your output should now look like the following:
first line = Hi My Name is Name
second line = Welcome to my simple program!
Improved Read Example – All lines at a time
Just as we improved our simpleRead.py with a context manager, we will go over doing the same thing for our simpleRead.py and our allRead.py.
For this example, we have modified our improvedHello.txt to contain the following text.
The First Line The Second Line The Third Line The Last Line
you can enter the following code into improvedReadAll.py.
with open('improvedHello.txt') as simple_read:
all_lines = simple_read.readlines()
for i in range(len(all_lines)-1):
print("Printing value", i, " = ", all_lines[i])
We are now using our context manager to take care of closing our read session for us.
We have also improved our reader to take a dynamic and unknown amount of lines in the text file. The above code when run will produce the following output.
$ Printing value 0 = The First Line
Printing value 1 = The Second Line
Printing value 2 = The Third Line
Printing value 3 = The Last Line
Now to explain and clear up this line of code
for i in range(len(all_lines)-1)
We use a for
loop with a range()
and len()
function to get the number of values in our array all_lines and then go from 0 to that number minus one because our index start at 0 not 1 and use the index i
to pull each value from the array.
Python Write to Text File: Write and Read Example
So far we have been either writing or reading separately. For some programs, this may be all you need to do but others may require reading and writing to the same file. If you need to make updates or modifications to your file by processing and updating information then this section will help you grasp a bit of experience and an example.
We will have a text file named ‘readAndWrite.txt’ with ‘user:password’ combinations. The initial contents look like the following:
user1:password user2:abc123 user3:password1234 user4:qwerty123
We will also have our new programmed named ‘modify.py’ and the goal of it is to update each entry in the text file and replace the second value with the same amount of characters with ‘*’
For example user1:password -> user1:********. It’s a simple program to remove the password from this user list.
Note: Passwords for programs should never be stored in plain-text. Never.
Our modify.py looks like the following:
with open('readAndWrite.txt', 'r+') as modifier:
lines = modifier.readlines()
modifier.seek(0)
for i in range(len(lines)-1):
line = lines[i].replace('\n', '')
password = line.replace(line.split(':')[1], '*'*len(line.split(':')[1]))
modifier.write(password + '\n')
It won’t have an output but will instead manipulate the file. The readAndWrite.txt now looks like this.
user1:******** user2:****** user3:************ user4:*********
Write and Read Example – Code explained
We will break down the code as follows:
- First, open the file with read and write mode and the file object is stored into modifier.
with open('readAndWrite.txt', 'r+') as modifier:
- Then we read in all the lines from the file into our lines variable
lines = modifier.readlines()
- By default, our writing begins at the end of the file so we use seek() to move the cursor location.
In this instance, we move it to location 0 or the beginning of the file.modifier.seek(0)
- We iterate from 0 to the length-1 of entries in the array. The minus 1 is used because our index is zero-based.
for i in range(len(lines)-1):
- We replace the newline at the end of the line so we can manipulate only the text part of the line.
line = lines[i].replace('\n', '')
The next line is a bit more complicated so we will break it down in more detail.
- We define what we will replace and what we are going to replace it with
line.replace(what_to_replace, what_we_are_replacing_with)
- We split up the user:password and grabs the password portion of the split.
line.split(':')[1]
- Next, we need the length of the password string and to multiply ‘*’ to get a string full of *’s equal to the length of the password string.
'*'*len(line.split(':')[1])
- Now, we combine the explanation and we come to our line that replaces the password with *’s and places into the password variable
password = line.replace(line.split(':')[1], '*'*len(line.split(':')[1]))
- Lastly, we write the modified username password pair and a newline back into the text file.
modifier.write(password + '\n')
Conclusion to Python Write to Text File Tutorial
Now you have a better understanding about writing to a text file and reading from a text file. Although this was an introduction, it is important to practice what you’ve learned. If you want to pick up more python and learn about it well. Feel free to sign up for my Python Programming Crash Course below. Happy Coding!
Leave a Reply