Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Use Files In Python
JonahElias

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!

Text Files

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.

Python can run any type of file, like a javascript file or a C file.
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:

file_name.txt

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.

Example:

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.

Different Modes

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:

file=file_obj

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".

Python Files

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.

example:

file_name.py

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:

import file_name

To call a function from that file, you would type the file_name, a period, and then the function name, like this:

file_name.my_function(parameters)

When you put it together, it looks like this:

This calls a function from a different file

Real Example:

If I had a file named test.py that looked like this:

I would import the file and then call the function, like this:

Another way

This will run the python script so you don't have to call specific functions

If you still have any questions please comment them!

And if you liked the tutorial be sure to upvote it

Voters
multicoder2021
UnoriginalNam3
LOLGIRLlol26
Quickpy
DominicHuieWhit
catspython
AmadoAurelio
JonahElias
Comments
hotnewtop
multicoder2021

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.

DominicHuieWhit

thx im new to python and i looked at other to tutorials but none of them worked.

JonahElias

@DominicHuieWhit Happy to help

CodeLongAndPros

Just btw, Python can read any file, be it JSON, PNG, JPEG, Mpeg-three, wave, ogg vorbis, C, Python, java.

BartKeijzer
Flazer27

is it possible to run code from a file instead of only running the functions

multicoder2021

This is actually a VERY good tutorial!

RainbowBoy7324

thanks! the import os technique really helped me! :D

AndrewWingfield

can you call on variables from the other file? is it filename.variablename or just variablename?