Hello friends, today in this tutorial, we will learn about python file handling functions to open a file, read a file and write a file.
Python has built-in functions for creating, reading, and writing files. Using that functions, you can manipulate the files for reading, writing, and creating files in which mode, which means what is the permission.
Let’s first see the different modes of file handling in python that you can use to manipulate them. There are basic four modes for opening a file in python.
File Handling Modes in Python
- Read (“r”): This is the default mode for opening a file. It will open a file as read-only. If the file does not exist then it will return an error.
- Append (“a”): This mode will open a file for writing by appending the text at the end of the file. It will create a new file if it does not exist.
- Write (“w”): This mode same as “a” to open a file for writing but it will overwrite the existing content with new content. It will also create a new file if it does not exist.
- Create (“x”): This mode is basically used to create a new file but it will return an error if the same filename already exists.
In addition, there are two more flags that you can also use in the open()
function to tell how the file should be handled in binary or text mode.
Python can only handle two types of files, one is a normal text file and the second is a binary file. As you know text file is a normal text file that has EOL (End Of Line) and the binary file has 0 and 1.
- Text (“t”): This is the default flag mode to represent the text file. You don’t need pass in open() function with modes.
- Binary (“b”): This “b” flag mode represent the binary file. It is used in open() function to open the binary files (e.g: images, graphics)
See Also: Full Detail Guide – Python Sequences
Let’s start and see how to handle a file to open, read and write in python.
Open a File in Python
To open a file in python, we will use the built-in open()
function. This is the first step of file handling even if you are reading a file or creating a file.
The open()
function takes two parameters, one is the file name, and the second is an access mode. It returns a file object which has a further read(), write(), close() functions to handle the file.
Note: Access modes are basically types of operations, which means how the file will be used once it’s opened. See the end of the tutorial for more details about access modes.
Syntax
Let’s create a simple text file as “demofile.txt“. So simply open this file in your favorite editor and add dummy text to it.
See the following code of reference to open a file in python.
The above function will open the file as a read mode because the default value of the mode is “r” and you don’t need to specify it. You can open this file in other modes also, see the following examples.
So you can see the above different ways to open the files with different modes. There are more modes that you can use to manipulate the files. See the access mode explanation section.
Read a File in Python
Now we will see how we can read the file in python using the read()
function. As I mentioned above the open()
function returns an object and it has a read() method that we can use to read the file.
Syntax
See the following code reference to open a file and then read that file using the read()
method. There are some cases and different ways to read the file, we will see it with examples.
The above code will open the file and read it in a read-only mode. You can change the mode as you want to open the file.
Note: Make sure the text file should be in the same location as the python script code.
But if your file is in a different location then you will have to specify the full file path. See the following code as a reference.
Read Part of File in Python
As you know read()
method returns a whole text by default. Then let’s say, you want to read only some part of the file, then how you will do it?
That is very simple, you just have to specify the length of the characters and how many you want returns from a file using the read()
method.
So the above code will only read the first 10 characters of the file. You can also read the file line by line. Let’s see how we can do it.
Read Lines in Python
To read the file line by line in python, we will use the readline()
method. It will return one line at a time. See the code reference.
You can also this method multiple times to read another line. For example, if we call this method two times then it will return the first two lines.
See Also: String Concatenation in Python – Example Tutorial
Write to File in Python
To write in the file in python, we will use the write()
method. The write also returns by open()
method same as the read()
method.
Syntax
Let’s see how you can write a file in python. We will take two examples of writing the file, one by appending the text and the second is overwrite the file.
Write to File by Appending
To append the text in the file while writing, we will use the “a” mode. We will open a file using the open()
function with “a” mode.
The “a” (Append) mode will append your text to the end of the file. Let’s see the example of appending the text in a file in python.
The above code will write the line “Welcome to YourBlogCoach!” in “demofile.txt” at the end of the file. Now when you will read it, you will see the new line is there.
Note: You may also have noticed that we have used the close()
method to close the open file. And you should always close open files because sometimes changes do not reflect until you close them.
Write to File by Overwriting
Now we will see the example of overwriting the existing content with new content. To write the file in python by overwriting the content, we will use the “w” mode to open a file.
The “w” (Write) mode will open a file as writable and it will write the file by replacing the existing content with new content.
This will write the new text line in “demofile.txt” and delete all existing content. It overwrites the entire file.
See Also: How to Delete a Line in Text File Using Python?
Conclusion
So in this tutorial, you learned about file handling functions in python and how you can open, read and write a file in python using the open(), read(), and write() methods.
You also learned about different access modes to open a file and different methods to read the files in python and how to write in the file by appending a new text or overwriting the text.