DEV Community

Rain Leander
Rain Leander

Posted on

File Handling and I/O

File handling and I/O (input/output) are important concepts in programming. In Python, file handling and I/O are performed using built-in functions and modules. In this blog post, we will explore the basics of file handling and I/O in Python, including how to read and write files, handle exceptions, and perform input/output operations.

Reading and Writing Files

In Python, files are opened using the open() function. The open() function takes two arguments: the file name and the mode in which to open the file (r for read mode, w for write mode, and a for append mode).

For example, the following code snippet opens a file called example.txt in write mode and writes a string to the file:

file = open("example.txt", "w")
file.write("Hello, world!")
file.close()
Enter fullscreen mode Exit fullscreen mode

To read the contents of a file, you can use the read() method of a file object. The following code snippet opens the example.txt file in read mode and reads the contents of the file:

file = open("example.txt", "r")
contents = file.read()
print(contents)
file.close()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, world!
Enter fullscreen mode Exit fullscreen mode

Exception Handling

In Python, exceptions are errors that occur during the execution of a program. To handle exceptions, you can use the try and except statements. The try statement is used to execute a block of code that may raise an exception, and the except statement is used to handle the exception if it occurs.

For example, the following code snippet opens a file called example.txt in read mode. If the file does not exist, the except statement is executed and an error message is printed:

try:
    file = open("example.txt", "r")
    contents = file.read()
    print(contents)
    file.close()
except FileNotFoundError:
    print("File not found")
Enter fullscreen mode Exit fullscreen mode

Output:

File not found
Enter fullscreen mode Exit fullscreen mode

Input/Output Operations

In Python, there are several built-in functions and modules for performing input/output operations. The input() function is used to prompt the user for input, while the print() function is used to output data to the console.

For example, the following code snippet prompts the user for input and outputs the input to the console:

name = input("Enter your name: ")
print("Hello, " + name + "!")
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your name: Alice
Hello, Alice!
Enter fullscreen mode Exit fullscreen mode

The sys module is another useful module for performing input/output operations. The sys.stdout.write() function is used to output data to the console, while the sys.stdin.readline() function is used to read input from the console.

For example, the following code snippet prompts the user for input and outputs the input to the console using the sys module:

import sys

sys.stdout.write("Enter your name: ")
name = sys.stdin.readline().strip()
sys.stdout.write("Hello, " + name + "!\n")
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your name: Alice
Hello, Alice!
Enter fullscreen mode Exit fullscreen mode

In this blog post, we have explored the basics of file handling and I/O in Python. We have learned how to read and write files, handle exceptions, and perform input/output operations using built-in functions and modules. By mastering these concepts, you can create more dynamic and interactive programs that can interact with external resources and user input.

Top comments (0)