File IO

This blog will help you get a complete understanding of file I/O with examples. Having little knowledge of Python programming will be enough just to read an example and get 100% out of it, as it is more detailed. itself is more explanatory. For the first 30 seconds it may be a bit boring, but later it will be interesting as it is a detailed article on file I/O.

A file is all about to store data. we can have file with multiple extensions. e.g. txt, py, java, xls, ppt, etc.

A directory is the one which will be having multiple files and subdirectories.

Below are the operations which can be perform on file:

  1. Open File
  2. Read and write content
  3. Close file

Open File

Python is having open() function to open a file. While opening a file, we can provide an operation type for that file. It may be read-only (r), write-only (w), or (a) append mode. The Open function will return a File object.

Syntax

file = open(<file_name>)
file = open(<file_name>, <access_mode>)

Below is the example where we are trying to open file which doesn’t exist and same got handeled by exception handling

OUTPUT:

Open, Read and Close File Operations

Below is the example for opening a file using the open() function at line number 2.

At line 5, the read() function will read the entire contents of the file.

The read(2) function will read the first two letters from line 4 of the file.

After you’re done with read and write operations, close the file using the close() function to free the memory.

OUTPUT:

By default file opens in read only format and on being writing it gives an error.

Write File Operation

To write content to a file, Python provides the write() function. It should always open in write mode. The write() function will always erase and override the file content.

Python’s append() function is used to update or add new content on top of existing content.

OUTPUT:

Note: It should be noted that the content was written in the same line because we did not use “n” to move the cursor to the next line at line number 5.

readline() and readlines()

readline() function will return individua line.

readlines() will return list of remaining lines.

OUTPUT:

writelines()

writelines() function helps us to write list of lines in to the file. It doesn’t return anything.

OUTPUT:

tell() and seek()

seek() function will move the cursor to specific position and tell() will return the current position of the cursor.

Example

OUTPUT:

append() Function

append() function will add merge content in file. It will not erase or override though it will merge the content. For append the only change is require to declare it as append (a) as done below at line number 2.

OUTPUT:

Note: In above output, added Done text at line number 2 in place of override line1.

OS Module

OS Module by default given by Python and helps us to delete file and directory using remove() and rmdir(). It also helps us to check either the file exist or not.

remove()

remove() function helps us to delete file and exists() function is check if given file exist or not.
Below code will help us to delete hello.txt file.

OUTPUT:
File deleted.

remdir()

remdir() function helps us to delete directory/folder and exists() function is check if given folder exist or not.
Below code will help us to delete test directory.

OUTPUT:
Directory deleted.

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0