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…
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.
Until now your programs lost everything when they ended: variables only live while the program runs. Files let you store data…
Python offers several ways to read a file. Choose according to what you need to do with the content.
Writing to a file is as simple as reading it, but it has a couple of details worth knowing.
Once you know how to read and write, the exercises consist of transforming what was read. These are the patterns that repeat.
Files introduce errors that do not exist when you only work with variables. These are the most common ones.
A file stores bytes , not letters. An encoding is the table that says which bytes represent which characters. If you read with a…
Create a program in Python to request lines from the user and write them in 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…
Create a Python program that asks the user for lines until you press Enter.
Write a program in Python to read a text file and make a copy in another file by changing the lowercase letters to uppercase.
Create a program in Python to count the number of words stored in a text file.
Create a Python program that makes a copy of a text file but encrypted.
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…
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…
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…
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…
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…