File Handling in Python
A file is a named location on the disk which is used to store the data permanently. Users can easily handle the files, like read and write the files in Python. File handling functions are creating a file, writing into the file, reading the file, and updating the file. Files are generally used to store information fetched from the programs. There are a multitude of types of files out there: text files, image files, audio files, and PDF files, just to name a few. Files are of 2 types in Python such as normal text files and binary files. The bytes are the values that are stored on a physical storage device when a file is saved.
Hence, a file operation can be done in the following order:
- Open a file
- Read or write (perform operation)
- Close the file
File Path:
A string with the file location represented in this manner is called a file path.
For example, in windows OS the file path locate a text file named “asad.txt” that is stored in the Documents subfolder of the user directory for a user named Pakistan.
- C:\Users\Pakistan\Documents\asad.txt
Open a File:
A file operation starts with the file opening. File opening is done with the open() function in Python. When we use the open() function, that time we must be specified the mode for which the File is opening. The function returns a file object which can be used to perform various operations like reading, writing, etc.
Syntax: file = open(name, access mode)
The files can be accessed using various modes like read, write, or append. The following are some details about the access mode to open a file.
Access Mode | Description |
r | Open a file for read only. (Default) |
w | Open a file for writing only. |
a | Open a file for appending. |
t | Open a file in text format.. |
b | Open a file in binary format. |
+ | Open a file for both reading and writing. |
- Opening the file in write mode
file = open(“C:/PythonPrograms/sample.txt”, ‘w’)
- Opening the file in read mode
file = open(“C:/PythonPrograms/sample.txt”, ‘r’)
- We can also write the statement as
file = open(“C:/PythonPrograms/sample.txt”, mode= ‘r’)
f = open(“test.txt”, ‘r’)
f = open(“C:/users/Python/test.txt”, ‘w’) |
Reading Data from File:
To read the file, first, we need to open the file in reading mode.
- # To print the content of whole file
f = open(“test.txt”, ‘r’)
print(f.read())
Output:
Hello Python
Hello World
- # To print the content of whole file
f = open(“test.txt”, ‘r’)
print(f.readline())
Output:
Hello Python
Write Data to File:
- f1 = open(“test.txt”, w)
f1.write(“Islamabad is Capital of Pakistan.”)
Output:
Islamabad is Capital of Pakistan.
- f1 = open(“file1.txt”, “w”)
f1.write( “Python is a great language. \n Yeah its great!!\n”)
Output:
Python is a great language.
Yeah its great!!
The write() method does not add a newline character (‘\n’) to the end of the string
Close a File:
Every time when we open the file, as a good practice we need to ensure to close the file, In python, we can use close() function to close the file.
- f = open(“test.txt”, ‘r’)
print (f.read())
f.close()
Output:
Hello Python
Hello World
Create & Delete a File:
In python, we can create a new file using the open method.
f3 = open(“file.txt”, ‘w’)
f3.close()
Similarly, we can delete a file using the remove function
f3.remove(“file.txt”)