Python File Handling – 20 Assignments

Beginner → Intermediate → Advanced (Hints Hidden)

🟢 Beginner Level (1–7)

1. Read and Print File Content

Open a text file and print its full content.

Show Hint

Use open(), read(), and with.

2. Count Characters in a File

Calculate total characters present in a file.

Show Hint

Use len(file.read()).

3. Count Number of Lines

Count how many lines are in a file.

Show Hint

Use readlines() or loop.

4. Append User Input to File

Take user input and append it as a new line.

Show Hint

Use mode "a" and write().

5. Read First 10 Characters

Read only the first 10 characters of a file.

Show Hint

Use read(10).

6. Check File Open or Closed

Verify whether a file is open or closed.

Show Hint

Use file.closed.

7. Copy File Content

Copy content from one file to another.

Show Hint

Read source file, write to destination.

🔵 Intermediate Level (8–14)

8. Read File Line by Line

Print file content line by line.

Show Hint

Use a for loop on file object.

9. Use tell() to Track Cursor

Display cursor position before and after reading.

Show Hint

Use tell() before and after read().

10. Move Cursor Using seek()

Move cursor to a specific position and read content.

Show Hint

Use seek(position).

11. Overwrite File Content

Replace existing file content completely.

Show Hint

Use "w" mode.

12. Write Multiple Lines

Write a list of strings into a file.

Show Hint

Use writelines().

13. Insert Text at Middle

Insert text at a specific position.

Show Hint

Read → modify string → rewrite file.

14. Read File in Chunks

Read file content in chunks of fixed size.

Show Hint

Use read(10) inside a loop.

🔴 Advanced Level (15–20)

15. Login Activity Logger

Store username with login timestamp.

Show Hint

Use append mode + datetime.

16. Error Logging System

Catch errors and save them into a log file.

Show Hint

Use try-except and write error message.

17. File Backup Tool

Create a backup copy of an existing file.

Show Hint

Read source, write to backup file.

18. Remove Empty Lines

Clean a file by removing blank lines.

Show Hint

Filter lines and rewrite file.

19. Word Search with Line Number

Search a word and show the line number.

Show Hint

Use enumerate() while reading lines.

20. Binary File Reader

Read binary file data safely.

Show Hint

Use "rb" mode and read().