17
Lesson 28 of 44

Text Files

Text files are a common way to store and manage information in programming. You will learn to work with text files, from basic reading and writing to more advanced operations. Practice with Python exercises for managing text files. Learn how to create, update, and search in various ways within text files.

  • 6 theory articles
  • 11 practice exercises
Start lesson

Fundamentals concepts 6

Opening and closing text files

Until now your programs lost everything when they ended: variables only live while the program runs. Files let you store data…

Theory
Read more

Reading text files

Python offers several ways to read a file. Choose according to what you need to do with the content.

Theory
Read more

Writing and appending to files

Writing to a file is as simple as reading it, but it has a couple of details worth knowing.

Theory
Read more

Processing the content of a file

Once you know how to read and write, the exercises consist of transforming what was read. These are the patterns that repeat.

Theory
Read more

Common mistakes with text files

Files introduce errors that do not exist when you only work with variables. These are the most common ones.

Theory
Read more

Text encodings: UTF-8 and Latin-1

A file stores bytes , not letters. An encoding is the table that says which bytes represent which characters. If you read with a…

Theory
Read more

Practice exercises 11

Write to a text file

Create a program in Python to request lines from the user and write them in a text file.

Exercise Easy
Solve it

Read a text file

Create a Python program that reads a text file and displays all its lines on the screen. You can use the open() function inside a…

Exercise Easy
Solve it

Appending to a text file

Create a Python program that asks the user for lines until you press Enter.

Exercise Easy
Solve it

Convert a text file to uppercase

Write a program in Python to read a text file and make a copy in another file by changing the lowercase letters to uppercase.

Exercise Medium
Solve it

Count the words in a file

Create a program in Python to count the number of words stored in a text file.

Exercise Medium
Solve it

Encrypt a text file

Create a Python program that makes a copy of a text file but encrypted.

Exercise Medium
Solve it

Reverse text from a file

Create a Python program that makes a copy of a text file but reverse all its content. Create a file with the same name but with…

Exercise Medium
Solve it

Search lines in a text file

Create a Python program that reads the contents of a text file and stores its lines in a list of strings. Then ask the user for…

Exercise Medium
Solve it

More program

Create a Python program that works like the Unix more command. It displays the contents of a text file and asks the user to press…

Exercise Medium
Solve it

Characters and bytes

A character and a byte are not the same: in UTF-8 letters such as é take 2 bytes and other symbols up to 4. In Latin-1 each…

Exercise Medium
Solve it

Read a file of unknown encoding

Old text files are often in Latin-1 and modern ones in UTF-8. A byte such as 0xF1 is usually an error in UTF-8 and a valid letter…

Exercise Hard
Solve it