Python, a leading programming language, offers robust support for file input/output (I/O) operations, enabling data to be read or written from/to files. Understanding file I/O is crucial for data manipulation and storage in Python.

The standard library in Python provides several methods for performing file I/O operations. These operations involve file handling functions that allow you to open, read, write, close, and delete files. Let's dive into the key aspects of file I/O in Python.

Opening and Closing Files
Before performing any operation on a file, it's essential to open it using the open() function. This function takes two arguments - the filename (with absolute or relative path) and the mode in which the file should be opened.

Here's an example of opening a file in read mode ('r') and thenclosing it:
```python file = open('example.txt', 'r') # Perform operations file.close() ```
Using 'with' Statement

The recommended way to handle files in Python is by using the with statement. This ensures that the file is properly closed after the inner block of code, even if an error occurs.
Here's how to use 'with' statement for file I/O:
```python with open('example.txt', 'r') as file: # Perform operations ```
Supported File Modes

Python supports various file modes for opening a file. Some common modes are:
'r': Open file for reading (default mode)'w': Open file for writing (truncates the file first)'x': Open file for exclusive creation (fails if the file already exists)'a': Open file for appending data to the end of the file
Reading Files

After opening a file in read mode, you can read its content in several ways.
Reading the Whole File



















You can read an entire file using the .read() method, which returns the contents of the file as a single string:
```python with open('example.txt', 'r') as file: data = file.read() ```
Reading Line by Line
To read a file line by line, you can use the .readline() method or a for loop with the file object.
Here's an example using a 'for' loop:
```python with open('example.txt', 'r') as file: for line in file: print(line.strip()) # .strip() removes newline character ```
Writing Files
To write data to a file, you need to open it in write ('w'), append ('a'), or any other write mode.
Writing Data
You can write data to a file using the .write() method of the file object. If the file is opened in write mode, it will be overwritten if it already exists. If opened in append mode, data will be added to the end of the file.
Here's how to write data to a file:
```python with open('example.txt', 'w') as file: file.write('Hello, World!\n') ```
Writing Multiple Lines
To write multiple lines at once, you can use a 'for' loop or methods like .writelines().
Here's an example using a 'for' loop:
```python with open('example.txt', 'w') as file: for line in ['Line 1', 'Line 2', 'Line 3']: file.write(line + '\n') ```
Finally, always remember to close or use the with statement to manage your files properly, as improper closure can lead to data loss or corruption. Happy coding!