How To Use Files In Python
How To Use Files In Python
In this tutorial, I will show you:
- How to use text files.
- How to use python files.
If you like the tutorial be sure to upvote it!
What is a text file?
A text file is a file containing characters and text. Information is stored as text.
The difference between a file and a list or a string variable is that a file saves even after the program stops.
This happens because a file resides on your computer's hard drive.
Which means that if you need to save the information or save text, you can append or type it into a file so it stays there forever, or until you delete it.
But in this tutorial, I will just be covering text and python files.
How to use and implement a text file
First you need to add a file
Name the file something that relates to what it contains.
Also, you need to add a period and then the extension.
The extension defines what type the file is, like jpeg or png.
Another example is mp3 or mp4
The extension for a text file is "txt", so a text file would look like this:
This makes it so that someone who looks at your code knows it's a text file.
Reading A Text File
We can access a text file in 3 steps, the first step is opening the file.
To do this, you need to create a file object to create a connection between the program and the file (A file object is a variable type).
You can name the file object anything.
To open the file, use the open function shown below:
Notice how inside of the parenthesis the file name is inside quotation marks.
Also, the "r" that is inside the parenthesis is the mode.
The mode allows us to do certain things. "r" is for read.
There is also "a" for append and "w" for write.
I will go over these later in this tutorial
So the next step is to loop through the file using a for loop.
Let's say that I create a file called "years.txt", and the info is displayed below:
Now we will open the file and loop through it
Also, notice how there is a line of space between each line of text.
To remove the extra line, use the .strip() function as described below.
Reading isn't the only mode in text files. You can also write and append.
Appending to a file is adding text to the end of the file
Writing to a file is writing over all of the data that exists within the file, so the only thing left is what you are writing to it.
Appending To A File
To append to a file, you would go through the same process as reading a file, except you would change the mode, and then use the print function to print to the file.
So in this case I am printing the user's input to the file but I could also print random text.
Also, after you put what you want to print to the file, you would put a comma and then type:
The reason you would do this is that you need to tell the computer which file to print to.
Obviously, you wouldn't type file_obj you would just type whatever you named the file object
Writing To A File
Writing to a file is the same process except you have to change the mode from "a" to "w".
The reason you would create 2 Python files is to organize your code better, or if you need another class in your program
To create a file:
In a text file, you would use the .txt extension. But to create a python file, you have to use the .py extension. If you don't, then it is not a python file.
To call functions from that file, you will need to import it.
At the top of your main file (the one named main.py), type import, and then type the file name:
To call a function from that file, you would type the file_name, a period, and then the function name, like this:
When you put it together, it looks like this:
This calls a function from a different file
If I had a file named test.py that looked like this:
I would import the file and then call the function, like this:
This will run the python script so you don't have to call specific functions