Home [python] A Comprehensive Guide to File Handling and I/O in Python
Post
Cancel

[python] A Comprehensive Guide to File Handling and I/O in Python

Intro

Introduction

File handling in Python refers to the process of working with files - reading data from files, writing data to files, and performing various operations on files. Python provides built-in functions and modules for file handling and input/output operations. In this article, we will explore the various aspects of file handling and I/O in Python.

Opening and Closing Files

To open a file in Python, we use the open() function. The open() function takes two parameters - the file name and the mode in which the file should be opened. The modes include ‘r’ for reading, ‘w’ for writing, ‘a’ for appending, and ‘r+’ for reading and writing. After performing operations on a file, it is important to close it using the close() method to release system resources.

Example 1: Reading from a File

1
2
3
4
file = open('sample.txt', 'r')
data = file.read()
print(data)
file.close()

Reading from Files

There are different methods to read data from a file in Python. The read() method reads the entire contents of a file as a single string, while the readline() method reads one line at a time. Additionally, the readlines() method returns a list of lines from the file.

Example 2: Writing to a File

1
2
3
file = open('output.txt', 'w')
file.write('Hello, World!')
file.close()

Writing to Files

To write data to a file in Python, we need to open the file in write mode (‘w’), append mode (‘a’), or read/write mode (‘r+’). We can use the write() method to write data to the file. If the file doesn’t exist, Python will create it when opened in write or append mode.

Example 3: Appending to a File

1
2
3
file = open('output.txt', 'a')
file.write('\nAppending new content!')
file.close()

Appending to Files

To append data to an existing file, we open the file in append mode (‘a’). This allows us to add new data to the end of the file without overwriting existing content. It is important to note that when using append mode, the file pointer is positioned at the end of the file for writing.

Conclusion

File handling and I/O operations are essential for working with external files in Python. By understanding how to open, read, write, and append files, developers can manipulate file data efficiently. Remember to always close files after use to avoid resource leaks. Python’s file handling capabilities make it a versatile language for handling various file operations.

This post is licensed under CC BY 4.0 by the author.
Contents