PythonFile HandlingTry it Yourself - Practice Problems

Try It Yourself: File Handling Practice Problems

Practicing file handling is crucial for mastering Python. Below are problems ranging from basic to advanced levels. Many align with the CBSE Class 12 Computer Science syllabus, helping students strengthen their skills.


Problem 1: Basic File Reading

Question: Write a Python program to read the content of a file named hello.txt and display it.

Show Solution
with open("hello.txt", "r") as file:
    content = file.read()
    print("File Content:", content)

Problem 2: Counting Words in a File

Question: Write a Python program to count the number of words in a file named words.txt.

Show Solution
with open("words.txt", "r") as file:
    content = file.read()
    words = content.split()
    print("Number of words:", len(words))

Problem 3: Writing to a File

Question: Create a program to write the string “Welcome to pythonforall.com” into a file named output.txt.

Show Solution
with open("output.txt", "w") as file:
    file.write("Welcome to pythonforall.com")
print("Content written to output.txt")

Problem 4: Searching in a File

Question: Write a program to search for the word “Python” in a file named data.txt. Display how many times it appears.

Show Solution
with open("data.txt", "r") as file:
    content = file.read()
    count = content.lower().count("python")
    print("The word 'Python' appears", count, "times in the file.")

Problem 5: Handling Large Files

Question: Write a program to count the number of lines in a large file named large_data.txt without loading the entire file into memory.

Show Solution
line_count = 0
with open("large_data.txt", "r") as file:
    for line in file:
        line_count += 1
print("Total lines in large_data.txt:", line_count)

Problem 6: CSV File Handling (CBSE Class 12)

Question: Write a program to create a CSV file students.csv with columns Name, Roll Number, and Marks. Add at least three entries.

Show Solution
import csv
 
data = [
    ["Name", "Roll Number", "Marks"],
    ["Alice", "101", "85"],
    ["Bob", "102", "90"],
    ["Charlie", "103", "88"],
]
 
with open("students.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(data)
 
print("students.csv has been created.")

Problem 7: JSON File Handling

Question: Write a program to create a JSON file config.json containing configuration data such as app name, version, and developer name.

Show Solution
import json
 
config_data = {
    "app_name": "Python For All",
    "version": "1.0",
    "developer": "pythonforall.com"
}
 
with open("config.json", "w") as file:
    json.dump(config_data, file, indent=4)
 
print("config.json has been created.")

Problem 8: File Pointer Navigation

Question: Write a program to read the first 10 characters of a file named sample.txt, move the pointer to the 5th character, and then read the next 5 characters.

Show Solution
with open("sample.txt", "r") as file:
    print("First 10 characters:", file.read(10))
    file.seek(5)
    print("Next 5 characters from 5th position:", file.read(5))

Problem 9: Binary File Handling

Question: Write a program to create a binary file image.bin, write some binary data to it, and then read the data back.

Show Solution
# Writing binary data
with open("image.bin", "wb") as file:
    data = b"This is binary data from pythonforall.com"
    file.write(data)
 
# Reading binary data
with open("image.bin", "rb") as file:
    content = file.read()
    print("Binary content:", content)

Problem 10: Automating File Operations (Advanced)

Question: Write a program to merge the contents of two files, file1.txt and file2.txt, into a new file named merged.txt.

Show Solution
with open("file1.txt", "r") as file1, open("file2.txt", "r") as file2:
    content1 = file1.read()
    content2 = file2.read()
 
with open("merged.txt", "w") as merged_file:
    merged_file.write(content1 + "\n" + content2)
 
print("Files merged into merged.txt")

By practicing these examples, you’ll develop a strong foundation in file handling.


Pyground

Play with Python!

Output: