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!
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.
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.
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:
file_obj = open("filename.txt", "r") ^ This is the mode
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:
2000 2001 2002 2003 2004 2005
Now we will open the file and loop through it
file_obj = open("years.txt", "r") for line in file_obj: print(line) file_obj.close() # ^ use the .close() function to close the file object
output: 2000 2001 2002 2003 2004 2005
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.
file_obj = open("years.txt", "r") for line in file_obj: line = line.strip() ^ This takes away the whitespace print(line) file_obj.close()
output: 2000 2001 2002 2003 2004 2005
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.
user_input = input("Enter what you want to append to the file ") file_obj = open("file_name.txt", "a") ^ Notice how its an "a" for append print(user_input, file=file_obj) ^ This is to tell the program what file to print to file_obj.close()
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:
^ ^ This is the function This is the file
When you put it together, it looks like this:
import file_name file_name.my_function(parameters)
This calls a function from a different file
If I had a file named test.py that looked like this:
def display_text(): print("This is multi-file coding")
I would import the file and then call the function, like this:
# Importing the file import test # Calling the display_text function test.display_text()
Output: This is multi-file coding
import os os.system("python my_file.py")
This will run the python script so you don't have to call specific functions
If you still have any questions please comment them!
How do you check if a text file has a specific value?
Like, say I want to create a Python project, and I want to permanently save some username and password inputs in a text file, how do I check if the text file contains that specific username or password.