DO NOT READ THIS!!

--Originally published at Coding The Future

Original image by Romain Vignes.

Hello! Ok, I must begging by saying that reading this article will not kill you nor hurt you in any way. In fact, it will be beneficial! So why the title, you may ask? Just to catch your attention! ?

Today's topic is reading and writing text files. This is one of the most important aspects in programming, because if you think about it, 99% of things online are text-based. Think about it... although Google has the option to search through voice and image, 99.9% of the times you probably just type what you need to find and hit enter. See? This is why programs must be able to interpret text because we live in a text-based world.

I want to get things clear though... When I say text files, I am not referring to MS Word documents –those are processed word docs– instead of plain .txt files. Let's get going.

Opening text files

Let's begging by looking at how to open a text file, which is the first step to reading or writing on the file.

In order to do this, we will need to declare a variable where a text file object will be stored. To do so, use the open( ) method and pass the filename as a parameter.

textFile = open(loremIpsum.txt)

Reading text files

Once we have the text stored in textFile, our sample variable, we can read it to process its contents. To do so, we need to use the .read() function.

In the following sample code, I read the text file I previously imported and print it:

print(textFile.read())

As you can see, since the text file is already stored in the textFile variable, I use the function within the file object.

Processing files

By processing, mean not just reading the plain text, but doing stuff with it. For example, we can read line by line using the .readline() function, or even read word by word by reading the text and storing it in a variable, and then splitting it using the .split() function.

Take a look at this sample code, which splits the text into individual words and prints them:

textFile = open('loremipsum.txt') textFile = textFile.read()
for word in textFile.split(): print(word)

If you wanted to search for a word in specific you could use a for loop and compare every word to the desired one.

Anyways, the possibilities are endless.

Writing to Files

When we open a file, it is set to reading mode by default, but if we include an additional parameter, a simple 'w', we change to write and wipe mode. Take a look:

textFile = open('loremipsum.txt', 'w')

Once you have opened the file this way, using the .write() function, you can write text to the file. WACTCH OUT THOUGH! This method completely overwrites the previous text on the file.

If we wanted to add text, we would need to open the file and pass 'a' as a parameter instead of 'w'.

textFile = open('loremipsum.txt', 'a') textFile.write("I love Latin texts!")

Closing the file

Even though many people skip this last step, this last thing is a good practise. After you are done reading your file, it is important to .close() your file, because otherwise your changes may not be reflected.

Before leaving, I invite you to check out OpenTechSchool which really helped me refresh my memory on this topic.

That's it for now... Expect another blog post tonight, I'm really in a writing mode!

@emamex98

comments powered by Disqus

Additional sources:
StackOverflow 1
StackOwerflow 2