Files and Directory Management

Most applications need to process some input and produce some output based on that input. I/O (Input and Output) is used to process the input and produce the output. The IO module provides the Python interfaces to stream handling. At the top of the I/O hierarchy is the abstract base class IOBase . It defines the basic interface to a stream.

File Handling in Python

A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a ready made structure. Python gives you easy ways to operate these files. Generally we divide files in two categories, text file and binary file . Text files are simple text where as the binary files contain binary data which is only readable by computer. File handling in Python requires no importing of modules. The File object provides basic functions and methods necessary to manipulate files by default. In Python, file processing takes place in the following order.
  1. Open a file which returns a file handle
  2. Use the handle to perform read or write actions
  3. Close the file handle

File Access Mode

The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. Here is a list of the different modes of opening a file:

interact with files  and directories in Python?