File Handling

Golf Teachers
4 min readJun 26, 2021

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 - https://analyticsindiamag.com/cheatsheet-file-handling-in-python/

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 - https://www.businesshorsepower.com/to-get-the-most-out-of-your-team-ask-better-questions/?doing_wp_cron=1624720772.0655999183654785156250

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

Link - https://www.quora.com/What-is-the-actual-use-of-file-handling-in-any-language-What-various-things-can-we-do-with-it-in-Python

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

Modes:

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

Link - https://tutorial.eyehunts.com/python/python-file-modes-open-write-append-r-r-w-w-x-etc/
  1. r

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+”)

Functions:

These are functions you can use to handle your files.

Link - https://data-flair.training/blogs/file-handling-in-python/
  1. read(“The number of characters you want to read”)

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

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

f.read()

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”)

f.write(“Hi”)

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”)

f.readlines()

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”)

f.read(5)

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”)

f.close()

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

Link - https://www.youtube.com/watch?v=ixEeeNjjOJ0

--

--