Files Tutorial (in Python)!!
Files Tutorial (in Python)!!
Hello, Replit Community and everyone else looking at this tutorial. This tutorial is mainly working with files (in Python). Good luck, and I hope you learn something about this tutorial!
Files are good to work with. They can store lots of data, and you can read, write, or both from the file! For example, a file can store the scores of a game (
The parameters for the
open() function that is needed for the with statement is showed below.
Now, we will demonstrate how to work with with statements. Below, you can find lots of examples to help you with your understanding.
For each of the examples below, also make sure to check out what is in the files. You can find it right below the actual code goes.
file.text (You can name a file any name, mostly people use 'txt' type files, for text)
First, we use a with statement (basically assigning
open('file.txt', 'r'). Then, we use a for loop to break up the file into lines. Strangely, there is a extra character at the end of each line that is not the file text, so we do
num = num[:-1] to solve it (basically cutting off the last index of
To print this letter written in the file, we will do something similar to last time. However, we will use another method to open the file.
We printed the text inside
letter.txt, but with a new strategy. You see, the with statement just assigned variable
Writing files can seem a little tricky at first, but should be easier than reading files! Writing files are less commonly used, but it is still useful; maybe you will use it one day!
Let's say we have an empty file:
The code shown below will be for our
After the code is ran, the file
hello_world.txt will now have some text in it, 'Hello, world!'.
Note that we need to set the second parameter of
open() to 'w', so that it is in writing mode; if you set it to reading mode, then it would return a error.
Let's say you have a list of 10 numbers, and you want to dump all of the numbers into a file to store instead.
First, we create a list that contains 10 numbers. We start by using a with statement to open the file as writing mode, then we use a for loop to get every element from the list in order.
Reading And Writing Files
Reading and writing files at the same time can be quite confusing. However, it is quite useful! If you copy the code (and the file) in the Writing Files section (any of the examples in that part), you can probably notice that every time you run it, the file should be updated with text, then when you run it again, the file clears, and the cycle goes all over again.
When you write a file, it does that. However, if you read and write a file at the same time, you basically save the contents of the file (to read it), and write it too! The result is that the file will run on its own without clearing, which would be a good idea for saving scores and finding high scores! Good luck with it!
We want to make a function that saves a player's score (part 1) and another function that finds the high score (part 2), all using files. Have a thought about it, then look below for the answers!
That is the first function, for the first part. We use a with statement to open the file, then we write in it. Notice that we are in mode 'a'.
Now, let's try to get the high score, using the data from the file.
Now, let's test it!