Practice Top 30 Python Programming Questions on File Handling
Q1. Write a program to write the string "Hello, world!" to a file named output.txt.
- Input: None (write "Hello, world!" to output.txt)
- Expected Output: File output.txt contains the text:
Hello, world!
Q2. Write a program to read the contents of a file named input.txt. Assume the file contains the text "Python is awesome!".
- Input: File input.txt with content:
Python is awesome!
- Expected Output: "Python is awesome!"
Q3. Write a program to write the following lines to a file named data.txt:
Line 1
Line 2
Line 3
- Expected Output: File data.txt contains:
Line 1
Line 2
Line 3
Q4. Write a program to read all lines from a file named data.txt into a list. Assume the file contains the text:
Line 1
Line 2
Line 3
- Input: File data.txt with content above
- Expected Output: ['Line 1\n', 'Line 2\n', 'Line 3\n']
Q5. Write a program to append the line "Line 4" to an existing file named data.txt.
- Input: File data.txt initially contains:
Line 1
Line 2
Line 3
- Expected Output: File data.txt contains:
Line 1
Line 2
Line 3
Line 4
Q6. Write a program to open a file in read mode (r) and print its contents. Assume readme.txt contains:
Welcome to Python!
- Input: File readme.txt
- Expected Output: "Welcome to Python!"
Q7. Write a program to open a file in write mode (w), write "Overwritten content" to it, and close the file.
- Input: File file.txt initially contains any content
- Expected Output: File file.txt now contains:
Overwritten content
Q8. Write a program to open a file in binary append mode (ab) and append the bytes b'\x00\x01\x02' to it.
- Input: File binary.bin initially empty
- Expected Output: File binary.bin contains the bytes:
00 01 02
Q9. Write a program to open a file in binary read mode (rb) and print its byte content. Assume binary.bin contains the bytes b'\x48\x65\x6C\x6C\x6F'.
- Input: File binary.bin
- Expected Output: b'Hello'
Q10. Write a program to create a new file named newfile.txt if it doesn’t exist and write "This is a new file" to it.
- Input: None
- Expected Output: File newfile.txt contains:
This is a new file
Q11. Write a program to handle the FileNotFoundError if a file named missing.txt does not exist when trying to read it.
- Input: File missing.txt does not exist
- Expected Output: "File not found!"
Q12. Write a program to handle the PermissionError if trying to write to a read-only file.
- Input: File readonly.txt is read-only
- Expected Output: "Permission denied!"
Q13. Write a program to check if a file is properly closed after reading its contents.
- Input: File example.txt contains:
Example content
- Expected Output:
Example content
File is closed: True
Q14. Write a program to read the contents of a file safely using the with statement. Assume notes.txt contains:
Python is great!
- Input: File notes.txt
- Expected Output: "Python is great!"
Q15. Write a program to handle both FileNotFoundError and PermissionError while trying to open a file.
- Input: File nonexistent.txt does not exist
- Expected Output: "File not found!"
Q16. Write a program to read the first 10 characters from a file named story.txt. Assume the file contains:
Once upon a time, in a faraway land...
- Input: File story.txt
- Expected Output: "Once upon "
Q17. Write a program to write a list of strings, each on a new line, to a file named lines.txt. Use the list ["Apple", "Banana", "Cherry"].
- Input: None
- Expected Output: File lines.txt contains:
Apple
Banana
Cherry
Q18. Write a program to read a file named poem.txt and print its contents line by line. Assume the file contains:
Roses are red,
Violets are blue.
Python is fun,
And so are you!
- Input: File poem.txt
- Expected Output:
Roses are red,
Violets are blue.
Python is fun,
And so are you!
Q19. Write a program to count the total number of lines in a file named data.txt. Assume the file contains:
Line 1
Line 2
Line 3
- Input: File data.txt
- Expected Output: 3
Q20. Write a program to copy the contents of a file named source.txt to a new file named destination.txt.
- Input: File source.txt contains:
This is the source file.
- Expected Output: File destination.txt contains:
This is the source file.
Q21. Write a program to read a file named image.png in binary mode and count the total number of bytes. Assume the file contains 1024 bytes.
- Input: File image.png
- Expected Output: 1024
Q22. Write a program to append the following lines to a file named log.txt:
Log Entry 1
Log Entry 2
- Input: File log.txt initially contains:
Previous log entries
- Expected Output: File log.txt contains:
Previous log entries
Log Entry 1
Log Entry 2
Q23. Write a program to create a binary file named data.bin and write the bytes b'\x01\x02\x03' to it.
- Input: None
- Expected Output: File data.bin contains:
01 02 03
Q24. Write a program to read a file in binary mode and decode its contents as UTF-8. Assume utf8.txt contains:
Hello, world!
- Input: File utf8.txt
- Expected Output: "Hello, world!"
Q25. Write a program to open a file in read/write mode (r+), read its contents, and append "Appended text" at the end.
- Input: File notes.txt contains:
Initial content
- Expected Output: File notes.txt contains:
Initial content
Appended text
Q26. Write a program to handle an IOError if it occurs while reading a file.
- Input: Simulate a hardware-related error (e.g., file locked)
- Expected Output: "An IOError occurred!"
Q27. Write a program to handle the error when trying to write to a directory path instead of a file.
- Input: Path dir_path/
- Expected Output: "Cannot write to a directory!"
Q28. Write a program to check if a file exists before attempting to read it.
- Input: File check.txt does not exist
- Expected Output: "File does not exist!"
Q29. Write a program to handle an error if there isn’t enough disk space while writing a file.
- Input: Simulate insufficient disk space
- Expected Output: "Not enough disk space!"
Q30. Write a program to safely delete a temporary file after its use, even if an exception occurs during file operations.
- Input: Temporary file tempfile.txt contains:
Temporary content
- Expected Output: File tempfile.txt is deleted after execution.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!