Python File Handling Practice Questions with Solutions

File handling allows you to create, read, write, update, and delete files using Python. It is one of the most important concepts for automation, data processing, and real-world Python applications. In this practice set, you’ll solve beginner-friendly file handling programs with complete solutions. Python File Handling Practice Questions with Solutions help to understand the concepts.


1. Python Program to Create and Write to a File

Difficulty: Foundation

Problem Statement

Write a Python program to create a file named student.txt and write "Welcome to CodeMantra" into it.

Python Solution

file = open("student.txt", "w")

file.write("Welcome to CodeMantra")

file.close()

print("File created successfully.")

Sample Output

File created successfully.

Explanation

The "w" mode creates a new file if it doesn’t exist and writes data to it.

Concepts Covered

  • open()
  • write()
  • close()

2. Python Program to Read Data from a File

Difficulty: Foundation

Problem Statement

Write a Python program to read the contents of student.txt.

Python Solution

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

content = file.read()

print(content)

file.close()

Sample Output

Welcome to CodeMantra

Explanation

The read() method reads the complete contents of a file.

Concepts Covered

  • read()
  • Read Mode

3. Python Program to Append Data to a File

Difficulty: Foundation

Problem Statement

Write a Python program to add "Python File Handling" to the end of student.txt.

Python Solution

file = open("student.txt", "a")

file.write("\nPython File Handling")

file.close()

print("Data appended successfully.")

Sample Output

Data appended successfully.

Explanation

The "a" mode appends new data without deleting existing content.

Concepts Covered

  • Append Mode
  • write()

4. Python Program to Read a File Line by Line

Difficulty: Foundation

Problem Statement

Write a Python program to read every line of a file.

Python Solution

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

for line in file:
    print(line.strip())

file.close()

Sample Output

Welcome to CodeMantra
Python File Handling

Explanation

The loop reads one line at a time from the file.

Concepts Covered

  • for Loop
  • Line-by-Line Reading

5. Python Program to Count the Number of Lines in a File

Difficulty: Practice

Problem Statement

Write a Python program to count the total number of lines in a file.

Python Solution

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

count = len(file.readlines())

print("Total Lines:", count)

file.close()

Sample Output

Total Lines: 2

Explanation

The readlines() method returns all lines as a list.

Concepts Covered

  • readlines()
  • len()

6. Python Program to Count the Number of Words in a File

Difficulty: Practice

Problem Statement

Write a Python program to count the total number of words in a file.

Python Solution

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

content = file.read()

words = content.split()

print("Total Words:", len(words))

file.close()

Sample Output

Total Words: 5

Explanation

The file content is split into words, and len() counts them.

Concepts Covered

  • split()
  • Word Count

7. Python Program to Check Whether a File Exists

Difficulty: Practice

Problem Statement

Write a Python program to check whether student.txt exists.

Python Solution

import os

if os.path.exists("student.txt"):
    print("File exists.")
else:
    print("File not found.")

Sample Output

File exists.

Explanation

The os.path.exists() function checks whether the specified file is available.

Concepts Covered

  • os.path.exists()

8. Python Program to Copy the Contents of One File to Another

Difficulty: Challenge

Problem Statement

Write a Python program to copy the contents of student.txt into backup.txt.

Python Solution

source = open("student.txt", "r")
destination = open("backup.txt", "w")

destination.write(source.read())

source.close()
destination.close()

print("File copied successfully.")

Sample Output

File copied successfully.

Explanation

The program reads data from one file and writes it into another.

Concepts Covered

  • File Copy
  • read()
  • write()

9. Python Program to Delete a File

Difficulty: Challenge

Problem Statement

Write a Python program to delete backup.txt.

Python Solution

import os

if os.path.exists("backup.txt"):
    os.remove("backup.txt")
    print("File deleted successfully.")
else:
    print("File not found.")

Sample Output

File deleted successfully.

Explanation

The os.remove() function deletes a file from the system.

Concepts Covered

  • os.remove()

10. Python Program to Read a File Using the with Statement

Difficulty: Challenge

Problem Statement

Write a Python program to read a file using the with statement.

Python Solution

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

Sample Output

Welcome to CodeMantra
Python File Handling

Explanation

The with statement automatically closes the file after use, making the code cleaner and safer.

Concepts Covered

  • with
  • Context Manager
  • open()

Chapter Summary

After completing this chapter, you have learned:

  • Creating files
  • Reading files
  • Writing files
  • Appending data
  • Reading files line by line
  • Counting lines
  • Counting words
  • Checking file existence
  • Copying files
  • Deleting files
  • Using the with statement

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top