0% found this document useful (0 votes)
3 views1 page

Python File Handling Basics and Examples

The document provides an overview of file handling in Python, detailing how to create, read, write, and modify files. It outlines various file opening modes and includes examples of writing to and reading from a file. Additionally, it presents assignment questions aimed at practicing file handling concepts, such as counting words in a file and using the 'with' statement.

Uploaded by

vishjos143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Python File Handling Basics and Examples

The document provides an overview of file handling in Python, detailing how to create, read, write, and modify files. It outlines various file opening modes and includes examples of writing to and reading from a file. Additionally, it presents assignment questions aimed at practicing file handling concepts, such as counting words in a file and using the 'with' statement.

Uploaded by

vishjos143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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().

You might also like