File Handling

Now coming to the stage, we have Mr.Questionasker. Ok uh . . . oh . . . uh . . . yeah. Um . . . so the uh . . . the question of the day is uh . . . what is file handling?

Good question Mr. Questionasker. What is file handling?

Link -

File handling is basically handling files. File handling is how you use files. You can read, write, open, close, and append files. Like what if you have a text file called Notes.txt. One day you wanted to open it and read it, but it didn’t open manually. So what would you do? Give up? But we are programmers we don’t give up we find a way to fix the computer in a way that we don’t break it, but solve the task ahead of us. The only way we can do this is to open up your file wherever you write your code and solve your problem. But how do we do that?

Link -

There are 3 ways to handle your file. Actually, 5 because you need to open and close your file.

Link -

If you want to handle your file you got to know some functions and modes you can use.


The mode specifies what type of functions you want to use.

Link -

This mode specifies that you want to read the file

open(“Example.txt”, “r”)

2. w

This mode specifies that you want to write the file.

open(“Example.txt”, “w”)

3. a

This mode specifies that you want to add on or append to the file.

open(“Example.txt”, “a”)

4. r+

This mode specifies that you want to read and write the file.

open(“Example.txt”, “r+”)

5. w+

This mode specifies that you want to write and read the file.

open(“Example.txt”, “w+”)

6. a+

This mode specifies that you want to append and read your file.

open(“Example.txt”, “a+”)


These are functions you can use to handle your files.

Link -

This function reads a file. The default parameter is to read all the characters.

f=open(“Example.txt”, “r”)

2. write(“The string you want to write”)

This function writes a file. You can write what you want to write in quotation marks inside the parenthesis.

Note: if you use the write() function again, it will write over your code. You need to change the mode to a or a+ to append to the file. But you still use the write() function.

f=open(“Example.txt”, “w”)


3. readlines(“No parameters”)

This function returns a list of all the lines in the file. The list will also contain the escape characters written in your file.

f=open(“Example.txt”, “r”)


4. readline(“The line you want to read”)

This function reads a particular line in a file. You can specify the line you want to read in the parenthesis.

f=open(“Example.txt”, “r”)

4. open(“File”, “Mode”)

This function opens a file. If the file is not found, it will create a new file. If the file is in a different folder, you need to specify the pathway.

f=open(“Example.txt”, “r”)

5. close(“No parameters”)

This function closes the file. When you are running the file, many applications are using the file. So, you need to close your file or your work may not be saved.

f=open(“Example.txt”, “r”)


That’s it for file handling. Toodle-oo!

Link -



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store