Cross_Column

Monday, 19 January 2026

File Handling in Python




File Handling in Python

File Handling in Python allows us to create, read, write, and modify files. It is widely used to store data permanently, read configuration files, generate reports, and work with logs.


Why File Handling is Important?

  • Stores data permanently
  • Reads and writes large data efficiently
  • Used in automation and testing frameworks
  • Works with logs, reports, and test data

Types of Files in Python

  • Text Files (.txt, .csv, .log)
  • Binary Files (.pdf, .jpg, .exe)

Opening a File in Python

Python provides the open() function to work with files.


file = open("data.txt", "r")

File Modes

Mode Description
r Read (default)
w Write (overwrites file)
a Append data
x Create file
b Binary mode

Reading Files in Python

Read Entire File


file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

Read Line by Line


file = open("data.txt", "r")
for line in file:
    print(line)
file.close()


Writing to a File


file = open("data.txt", "w")
file.write("Welcome to Python File Handling")
file.close()

Note: Mode w will overwrite existing data.


Appending Data to a File


file = open("data.txt", "a")
file.write("\nNew line added")
file.close()


Using with Statement

The with statement automatically closes the file.


with open("data.txt", "r") as file:
    print(file.read())


Reading File Using read(), readline(), readlines()


file.read()       # Reads entire file
file.readline()   # Reads single line
file.readlines()  # Reads all lines as list


File Handling with Exception Handling


try:
    with open("data.txt", "r") as file:
        print(file.read())
except FileNotFoundError:
    print("File not found")


Working with Binary Files


file = open("image.jpg", "rb")
data = file.read()
file.close()


Delete a File in Python


import os

if os.path.exists("data.txt"):
    os.remove("data.txt")
else:
    print("File does not exist")


File Handling in Automation Testing


with open("testdata.txt", "r") as file:
    username = file.readline()
    password = file.readline()

Used in:

  • Reading test data
  • Writing test execution logs
  • Storing API responses
  • Generating reports

Best Practices

  • Always close files or use with
  • Handle exceptions properly
  • Use correct file mode
  • Avoid hardcoding file paths

Common Interview Questions

  • What is file handling?
  • Difference between read() and readline()
  • What is with statement?
  • Difference between w and a mode?
  • How to handle file exceptions?

Conclusion

File handling in Python is essential for data storage and automation tasks. Mastering this concept helps in building powerful Python applications and test automation frameworks.

👉 Learn more Python tutorials on way2testing.com

No comments:

Post a Comment

Few More

Multithreading and Multiprocessing in Python

Multithreading and Multiprocessing in Python Python provides powerful tools to perform multiple tasks simultaneously usi...