Python File Handling - Assignment with Examples
1. What is File Handling?
File handling in Python allows you to create, read, write, and modify files. It is used to store data
permanently. 2. File Opening Modes:
• r – Read mode
• w – Write mode
• a – Append mode
• r+ – Read and Write
• b – Binary mode
3. Example: Writing to a File
f = open("[Link]", "w") [Link]("Hello, File Handling!") [Link]() 4. Example: Reading from a
File
f = open("[Link]", "r") print([Link]()) [Link]() 5. Using with Statement
with open("[Link]", "a") as f: [Link]("Adding new line") 6. Assignment Questions
1. Write a Python program to create a text file and write 5 lines of data.
2. Write a program to read the content of a file line by line.
3. Write a program to count the number of words in a file.
4. Write a program to copy data from one file to another.
5. Write a program using 'with' statement to append user input into a file.
6. Explain read(), readline(), readlines() with examples.
7. Write a program to display the file pointer position using tell() and move it using seek().