File handling in Python enables users to read and write files, besides various other file handling options. There is nothing required for importing an external library to read and write files in Python. It makes available an inbuilt function for creating, writing, and reading files.
The idea of file controlling has extended over diverse languages. This programming language has many distinctive features and functions to look out of files. It differentiates other high-level programming languages on the source of the structural organization of file management. It is simple to learn and appliance the coding module in Python. In this article, we would learn file handling in Python with depth.
Opening a Text File in Python
We require to use the built-in open function to open a file. The Python file open function yields a file object. That comprises methods and attributes to apply several operations for opening files in Python.
Syntax of Python open file function
file_object = open ("filename", "mode")
- We may identify the mode while opening a file.
- We insist on whether it wants to read r, write w or append a to the file in mode.
- We may similarly identify if it wants to open the file in text mode or binary mode.
- Reading in text mode is by default.
- We receive strings when reading from the file in this mode.
- This is important that Python treats files in a different way as text or binary.
- Binary mode returns bytes. This is the mode to be used when commerce with non-text files similar to images or executable files.
- The mode in the open function syntax would convey Python as what operation we want to make sure of on a file.
- ‘r’ for reading Mode: This is used only to read data from the file.
- ‘w’ for Write Mode: This is used when we want to write data into the file or modify it. This mode overwrites the data existing in the file.
- ‘a’ for Append Mode: This is used to append data to the file. Data would be appended at the end of the file pointer.
- ‘r+’ for reading or Write Mode: This is used when we need to write or read the data from the same file.
- ‘a+’ for Append or Read Mode: This is used when we desire to read data from the file or append the data into the same file.
# a file named "tech", will be opened with the reading mode. file = open('tech.txt', 'r') # This will print every line one by one in the file for each in file: print (each)
Creating a Text File in Python
We can create a .text files (tech.txt) by using the code with Write to file Python, we have confirmed here:
Open the .txt file
- We stated the variable f to open a file named tech.txt.
- Open takes two arguments; (1) the file that we want to open (2) a string that denotes the kinds of permission or operation we want to do on the file
- We used the “w” letter in the argument that shows Python write to file.
- It will create a file in Python if it does not be present in the library.
- “+ “sign shows together to read and write for Python create file operation.
Enter data into the file
for i in range (10): f.write("This is line %d\r\n" % (i+1))
- Here, we have a for a loop. It runs over a range of 10 numbers.
- With the write function to enter data into the file.
- “This is line number” is the output we want to iterate in the file.
- With Python, we declare the write file function.
- Then % d that displays integer.
- Therefore, essentially we are putting in the line number that we are writing.
- Then put it in a way of moving return and a new line character
Close the file instance
# Python code to create a file file = open('tech.txt','w') file.write ("This is the write command") file.write ("It enables us to write in a specific file") file.close()
Appending to a File in Python
We may append or add a new text to the already present file or a new file.
- The “+” sign in the code specifies that it will create a new file if it does not occur.
- Though, we have already the file.
- Therefore, we are not essential to create a new file for Python append to file operation.
for i in range(2): f.write ("Appended line %d\r\n" % (i+1))
This would write data into the file in append mode.
Reading Files in Python
- We may read a file in Python by calling the .txt file in a “read mode”(r).
Open the file in Reading mode
f=open ("tech.txt", "r")
- We can use the mode function in the code for checking that the file is in open mode.
- We proceed ahead, if Ok.
if f.mode == 'r':
- To read file data use f.read.
- Keep it in variable content for reading files in Python.