File Handling

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
Search for a command to run...

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
No comments yet. Be the first to comment.
This series will provides the core concepts of python programming starting from variables, data types, control flow, and functions to some problem solving.
Errors and Exceptions in Python are mechanisms for handling issues that arise during program execution. Errors indicate code issues that prevent successful execution, while exceptions are events that disrupt the normal flow of code. Error types Synta...
Bias in Machine Learning Bias refers to the simplifying assumptions made by a model to make the target function easier to learn. High bias can lead to underfitting Represents the error introduced by approximating a real-world problem Example: A li...
When it comes to developing software applications, choosing the appropriate architectural style is a critical decision. Among the popular options are microservices and monolithic architecture. By understanding the trade-offs, readers can make well-i...

In a world where information is everywhere, businesses have tons of data at their fingertips. This data is like a goldmine waiting to be used for smart decisions. That's where Big Data analytics and Data Science come in. They're like the experts who ...

Python is a powerful tool for data professionals, largely due to its extensive collection of open-source libraries and packages. In this blog, we'll explore the fundamentals of pandas, with a special focus on its core data structures: Series and Data...

NumPy is a powerful library for numerical computing in Python, offering robust support for large, multi-dimensional arrays and an extensive collection of mathematical functions. Creating Arrays To begin using NumPy, import the library and create arra...

File handling is a fundamental aspect of programming, allowing us to read and write data to and from files.
Before performing any file operation, we need to open the file. Python provides the open() function for this purpose. The open() function takes two arguments: the filename and the mode in which the file should be opened. The mode can be specified as "r" for reading, "w" for writing, or "a" for appending to an existing file.
file_read = open("example.txt", "r") # Read mode
file_write = open("example.txt", "w") # Write mode
file_append = open("example.txt", "a") # Append mode
file_binary = open("example.bin", "rb") # Binary read mode
file_text = open("example.txt", "rt") # Text read mode
file_update = open("example.txt", "r+") # Read and update mode
file.close() # Close the file
Using with keyword. When we use this keyword we do not need to explicitly close our .file
with open("example.txt", "r") as file:
content = file.read()
print(content)
Python offers different methods for reading data from files.
To read the entire contents of a file, we can use the read() method. It reads the entire file content as a string.
'''First create example.txt'''
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
To read a file line by line, we can use a for loop. Each line can be accessed as a string.
file = open("example.txt", "r")
for line in file:
print(line)
file.close()
If we want to read a specific number of characters from a file, we can use the read(n) method, where n represents the number of characters to be read.
file = open("example.txt", "r")
content = file.read(10) # Read first 10 characters
print(content)
file.close()
Python provides several methods for writing data to files. Let's explore some of the common approaches:
To write data to a file, open the file in write mode ("w") or append mode ("a") and use the write() method.
file = open("example.txt", "w")
file.write("Hello, World!")
file.close()
'''
Output open example.txt:
-------------------------
Hello, World!
'''
This code creates a new file called "example.txt" and writes the text "Hello, World!" to it. If the file already exists, it will be overwritten.
If we want to add content to an existing file without overwriting the existing data, we can open the file in append mode ("a") and use the write() method.
file = open("example.txt", "a")
file.write("This is an appended line.")
file.close()
'''
Output : open example.txt
-------------------------
Hello, World!
This is an appended line.
'''