What is File Handling?
- Every web app requires the handling of files. File handling is critical when data must be stored permanently in a file.
- A file is a named location on the disc where related information is stored.
- After the program has terminated, we can access the stored information (non-volatile).
File Access Modes:
Access Modes govern the activities you can perform on the opened file. These describe how to use the file once it has been opened. These modes also specify the location of the file handle within the file. A file handle, like a pointer, indicates where data should be read or written to the file.
In Python, the following are the details about the access mode :
Mode | Description |
---|---|
r | Reads from a file and returns an error if the file does not exist (default). |
w | Writes to a file and creates the file if it does not exist or overwrites an existing file. |
x | Exclusive creation that fails if the file already exists. |
a | Appends to a file and creates the file if it does not exist or overwrites an existing file. |
b | Binary mode. Use this mode for non-textual files, such as images. |
t | Text mode. Use only for textual files (default). |
+ | Activates read and write methods. |
Opening a File:
Python has an open() function that takes two arguments: the file name and the access mode in which the file is accessed. The function returns a file object that can be used to perform operations such as reading, writing, and so on.
Syntax:
file object = open(<file-name>, <access-mode>, <buffering>)
The files can be accessed using various modes like read, write, or append as mentioned above.
Example:
fileptr = open("file1.txt","r")
if fileptr:
print("file is opened successfully")
In the preceding code, we passed filename as the first argument and opened the file in read mode by specifying r as the second argument. The fileptr holds the file object, and if the file is successfully opened, the print statement is executed.
Closing a File:
The close() method of a file object flushes any unwritten data and closes the file object, preventing further writing.
When a file’s reference object is reassigned to another file, Python automatically closes the file. It is best to use the close() method to close a file.
Syntax:
fileobject.close()
Example:
fileptr = open("file1.txt","r")
if fileptr:
print("file is opened successfully")
#closes the opened file
fileptr.close()
We cannot perform any operations on the file after it has been closed. The file should be properly closed. If an exception occurs while performing some file operations, the program exits without closing the file.
Note: also read about Python Exception Handling – III(Raise Keyword )
Follow Me
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment