Python Read File and Write to File (with SEO Examples)
About 40 minutes
- Variable
- String
- Integer
- Float
- Boolean
- List
- Dictionary
- type
- key
- index
- append
- len
- if
- else
- elif
- Comparison Operators
- and
- or
- not
- for
- while
- break
- continue
- Function
- def
- Parameter
- Argument
- return
- set
- open
- with
- read
- write
- strip
The counter at the top counts this lesson's exercises (2 of them). Reading and running the examples don't count.
In the previous lessons, all the data we worked with, we typed into the code ourselves.
For example, if we had a few URLs:
['https://example.com/', 'https://example.com/blog/', 'https://example.com/services/']
That was fine while we were learning Lists, Loops and Conditions, because there were only a few URLs and the goal was to understand Python itself.
But say you're working on a site and you exported 5,000 URLs from Screaming Frog.
Would it make sense to do this?
Of course not.
In real work, the data is usually in a file, and Python is the one that needs to go to that file and read the data.
And that's exactly what this lesson teaches.
We'll start with a very ordinary text file. Once the idea is clear, we'll use the same foundation later with bigger files that matter more in SEO, like CSV.
The first problem: how does Python read a file?
Make a new file called urls.txt and write these URLs in it:
Keep urls.txt in the same folder as the Python file you're working on. So if you have lesson6.py, the folder looks like this:
Now we have the data, but Python doesn't know anything about it yet.
We have to tell it to open the file first, and we do that with open(). Let's try the simplest example:
<_io.TextIOWrapper name='urls.txt' mode='r' encoding='utf-8'>
You might look at that output and say: what good is that to me?
Almost none, so far. We only opened the file; we haven't read what's in it yet.
Before we read it, let's understand this line first: file = open("urls.txt", "r", encoding="utf-8")
The open() function opens a file so Python can work with it. Inside it we wrote "urls.txt", which is simply the name of the file we want to open.
After it we wrote "r". The letter r comes from read. So we're telling Python:
As for encoding="utf-8", it matters so Python reads the text in the file correctly, especially when it has accented letters or text in other languages.
You don't need to go into the details of encoding right now. All you need to know is that we use utf-8 with text files to avoid problems with characters.
After opening the file, we kept the open file in a variable called file. So instead of telling Python to open urls.txt every time, we can work with it through the variable file.
But as we said, we only opened it. We need to read what's inside.
Reading the file's contents
To read the file's contents, we use read(). Let's try:
https://example.com/ https://example.com/blog/python-seo/ https://example.com/services/seo/ https://example.com/blog/technical-seo/
Now Python actually opened the file and read everything in it.
Look at this line: content = file.read()
Then print(content) printed the contents.
So why did we write file.close() at the end? Because once we've opened a file and worked with it, it's best to close it when we're done. Think of it as opening a door, doing what you came for, and closing it behind you.
But since we're still learning, I don't want you to keep worrying every time: I opened the file, did I close it? Python has a better way that lets you open a file and work with it while Python takes care of closing it for you. That's the way we'll use from here on.
Using with
Let's write the same example like this:
https://example.com/ https://example.com/blog/python-seo/ https://example.com/services/seo/ https://example.com/blog/technical-seo/
Same result. But notice we didn't write file.close(), because with makes Python close the file automatically once it's done with it.
Let's understand the line: with open("urls.txt", "r", encoding="utf-8") as file:
Then we have content = file.read(). Watch the indentation: this line is inside with. And when the indentation ends, at print(content), Python has finished with the file and closed it.
From here on, we'll use this way, with open(...), instead of the first one.
We have a new problem
We managed to read the whole file at once with content = file.read(). But in SEO, that's usually not what we want.
We have a file with four URLs, and we don't want the four URLs as one piece of text.
We want to handle each URL on its own. Why? Because soon we might say:
- If the URL has
/blog/in it, do something. - If the URL has
/product/in it, do something else. - Check each URL on its own.
- Save a particular group of URLs.
And here we can use the for loop we learned. Let's try:
https://example.com/ https://example.com/blog/python-seo/ https://example.com/services/seo/ https://example.com/blog/technical-seo/
Notice something odd. Why is there an empty line between each URL and the next?
We didn't tell print() to do that. The problem comes from the file itself.
Each URL is on its own line, so the end of the first URL has a line break. Python read that line break along with the text. Then print() itself adds a new line too. So we ended up going down a line twice.
We need to clean the text before we use it. And for that, we need something new.
strip()
Let's step away from files for a second and try it on simple text:
Python
There were spaces before and after the word Python. When we used strip(), it removed the spaces at the start and the end.
And the same idea helps with the line that comes from the file, because it removes the line break at its end. Let's try:
https://example.com/ https://example.com/blog/python-seo/ https://example.com/services/seo/ https://example.com/blog/technical-seo/
Now the result is right. Let's see what happens step by step.
We have for line in file:
In the loop's first round, the variable line holds the first line. Then we did url = line.strip(): we cleaned the line and kept the clean version in url. Then print(url) printed the URL. Then the loop goes back and takes the next line. And so on until the end of the file.
For the first time, we're handling each URL on its own. And that's exactly what we'll need in our work.
Back to lists
Right now we're reading the URLs and printing them. But instead of just printing them, I might want to put them in a List to use later. And that's something we already learned. We start with an empty list:
['https://example.com/', 'https://example.com/blog/python-seo/', 'https://example.com/services/seo/', 'https://example.com/blog/technical-seo/']
Notice that we're not learning Python from scratch here. We're stacking the things we learned on top of each other.
We learned before that urls = [] makes an empty list, and that append() adds an item to a list. All we did now is that the items are no longer typed into the code by hand. They come from a file. And that's a very important difference.
Before, we wrote the data. Now Python fetches the data from outside the program.
Let's do something for SEO
Our file now has four URLs, and we want only the blog post URLs.
We already learned if. So we can say:
/blog/ is in the URL, add the URL to the list.['https://example.com/blog/python-seo/', 'https://example.com/blog/technical-seo/']
Pay attention here, because this example matters. Let's follow Python in the order it runs.
- 1
We started with an empty list, blog_urls. - 2
We opened the file and took the first line: https://example.com/. Does it have /blog/? No, so it isn't added. - 3
The next URL: https://example.com/blog/python-seo/. Does it have /blog/? Yes, so it's added to the list. - 4
The next URL: https://example.com/services/seo/. No /blog/, so it isn't added. - 5
The last URL: https://example.com/blog/technical-seo/. It has /blog/, so it's added. - 6
So the final result has only the two URLs with /blog/ in them.
So far, we've taken a whole file and pulled out only the data we need.
But the result is still inside Python. What if we want to save it?
How does Python write a file?
Just as we used "r" for reading, we have "w" for writing. The letter w comes from write. Let's try a simple example first:
When you run this code, Python creates a file called result.txt with SEO Analysis Complete in it.
But as we agreed, in this course we don't write code and assume everything happened.
We'll read the file afterwards and print the result:
SEO Analysis Complete
Now we've seen for ourselves that Python really wrote the text into the file.
But there's something very important to know about "w": if the file already exists and has data in it, using w starts writing from scratch.
So if result.txt has Old Report in it and we run this code:
New Report
Old Report is gone. So remember:
rmeans read the file.wmeans write the file from scratch.
Which means you need to be careful when you use w on a file with important data in it.
Saving the blog URLs
Now we have the list blog_urls with two URLs in it. We want Python to make a file called blog_urls.txt and write the URLs in it. We could do:
https://example.com/blog/python-seo/https://example.com/blog/technical-seo/
There's a problem. The two URLs are stuck together.
That's expected, because we told Python file.write(url): write the URL. But we didn't tell it to go to a new line after writing it. And for that, we need to learn one very small thing: \n.
What does \n mean?
Let's try it away from files:
SEO Python
So \n tells Python: go to a new line. Instead of file.write(url), we'll write file.write(url + "\n"), meaning write the URL, then go to a new line. Let's try the full example:
https://example.com/blog/python-seo/ https://example.com/blog/technical-seo/
Now we have a tidy file, with each URL on its own line.
Putting it all into one program
Now, instead of typing the data by hand, we'll run the whole process from the file to the result.
What we want Python to do:
- Read the file.
- Take each URL on its own.
- Clean the URL.
- Pick the
/blog/URLs only. - Keep them in a list.
- Create a new file with the results.
- Print the results so we can see what happened.
Blog URLs: https://example.com/blog/python-seo/ https://example.com/blog/technical-seo/
At the same time, Python created a file called blog_urls.txt with the same URLs in it, one per line.
I want you to notice the shift that just happened. In the first lessons we did:
- Data written inside the code.
Pythonworks with it.print()
Now we do:
- A file with data in it.
Pythonreads it.Pythonanalyzes the data.Pythonpicks what we need.Pythonsaves the result to a new file.
And that's much closer to the way we'll actually work in SEO.
So what's the difference between w and a?
There's one more small thing we need to know before we close the topic of writing.
Say you have a file called report.txt with Page 1 in it. If we open it with "w" and write Page 2, the old contents are wiped.
But sometimes we don't want to replace the file; we want to add something to the end of it. That's when we use "a". The a comes from append, meaning add to what's there. Let's try:
Page 1 Page 2
First we used "w", so we created the file and wrote Page 1. Then we used "a", so Python kept what was there and added Page 2 at the end.
So the three you need to know for now:
rreads.wwrites from scratch.aadds to what's there.
You don't need to memorize dozens of file modes; these three are plenty for where we are now.
A file inside a folder
So far, urls.txt sits next to the Python file. But later you might organize your project like this:
Now if you write open("urls.txt"), Python looks for the file next to lesson6.py. But it isn't there; it's inside the data folder. So we have to say where it is:
https://example.com/ https://example.com/blog/python-seo/ https://example.com/services/seo/ https://example.com/blog/technical-seo/
The part data/urls.txt is called a path: the route Python takes to reach the file. We don't need to go deeper into paths right now; we'll come back to them when we need to.
Using a function from the last lesson
In the last lesson we learned functions.
So can we have a function whose job is to read blog URLs from any file? Yes. We write:
['https://example.com/blog/python-seo/', 'https://example.com/blog/technical-seo/']
Let's understand just the part that's new in this example.
We have get_blog_urls("urls.txt"). We send the function the file's name. It receives it as filename, whose value here is urls.txt. So when Python reaches with open(filename, ...), you can think of it as becoming with open("urls.txt", ...).
Then it does what we learned: reads each line, cleans it, checks whether it has /blog/, and adds it to the list. And at the end, return blog_urls sends the list back.
The point here isn't to make files complicated with functions. The point is for you to start seeing that the things we learned aren't separate lessons. We've started building a program using several parts of Python together.
A bigger SEO program
Now our urls.txt has six URLs, and we want to split them into blog URLs, product URLs and other URLs.
Before you look at the code, think it through yourself. How many lists do we need?
Three: blog_urls, product_urls, other_urls.
Then we read each URL. If it has /blog/, we add it to blog_urls. If it has /product/, we add it to product_urls. And if it's neither, we put it in other_urls.
Blog URLs: https://example.com/blog/seo/ https://example.com/blog/python/ Product URLs: https://example.com/product/laptop/ https://example.com/product/phone/ Other URLs: https://example.com/ https://example.com/about/
Notice we used elif and else, which we learned before. And this time we used \n inside print(): print("\nProduct URLs:") goes down a line before printing the heading, so the result is easier to read.
Saving the three groups
Now we don't want the results only on screen. We want three separate files, so we add:
And the full program becomes:
Blog URLs: https://example.com/blog/seo/ https://example.com/blog/python/ Product URLs: https://example.com/product/laptop/ https://example.com/product/phone/ Other URLs: https://example.com/ https://example.com/about/
And in the folder you'll find four files: urls.txt, blog_urls.txt, product_urls.txt and other_urls.txt.
You've actually built a small program here. It took data from a file, analyzed it by conditions you set, split it up, and saved the results in new files.
What you need to take from this lesson
You don't need to memorize the whole code. What matters is understanding how the pieces fit.
When you have a file and want to read it: with open("urls.txt", "r", encoding="utf-8") as file:
To read all the contents at once: content = file.read()
To handle each line on its own: for line in file:
If the line has spaces or a line break and you want to clean it: line.strip()
To write a new file: with open("result.txt", "w", encoding="utf-8") as file:
To add to an existing file instead of replacing it: with open("result.txt", "a", encoding="utf-8") as file:
And when you write several values that should each be on their own line: file.write(url + "\n")
Those are the basic tools you need from this lesson.
Practical test
pages.txt is ready for you: the test runs your code on the same contents shown below. The output stays hidden until you solve it, you can ask for a hint after your first try, and the full solution shows after two tries.This time the test runs on a real file. Create a file called pages.txt and write in it:
Question 1
Pull the product URLs out of a file
Write a program that opens pages.txt, reads each URL on its own and cleans it with strip(), and builds a list called product_urls with only the URLs that contain /product/. Print each URL in the list on its own line, then create a new file called product_urls.txt and save the same URLs in it, one per line.
Question 2
Split the file into three groups
Change the solution above so it splits pages.txt into three groups, Blog, Product and Other, and saves each group in its own file: blog_urls.txt, product_urls.txt and other_urls.txt.
If you managed this part yourself, you didn't just understand files. You started connecting files with lists, loops and conditions, and that's the core idea we need before we start working with bigger SEO data in the coming lessons.
You still have exercises left
0/2Exercises left: 2 of 2. The lesson counts as complete once you solve them all, and you can come back to them any time.
Well done, you reached the end of the lesson.