0% found this document useful (0 votes)
64 views

File Handling

Uploaded by

tvsaacademy6off
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

File Handling

Uploaded by

tvsaacademy6off
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FILE HANDLING

TEXT FILES
Write Python programs for the following:
1. To read the content of the file
2. To print the first 5 characters of the file
3. To count the number of the words in the file
4. To print the longest word in the file along with its length
5. To count the number of vowels (both uppercase and lowercase)
6. To count the number of uppercase letters in the file
7. To count the number of occurrences of the word ‘the’ as a whole word in the file.
8. To read and print the first line of the file
9. To print the longest line with its length
10. To count the number of lines in the file
11. To count the number of lines starting with the letter ‘A’
12. To display the alternate lines from a text file.
13. To display the words that are less than 4 characters in length.
14. To copy the words starting with capital letters into a new file
15. To copy the lines starting with the letter ‘T’ into a new file
16. To read the contents from a text file and reverse the lines and copy onto a new file
17. To copy the alternate lines from a file to a new file

CBSE Board question 2022-2023


18. Wite the definition of a Python function named LongLines() which reads the contents of a
text file named 'LINES.TXT' and displays those lines from the file which have at least 10
words in it. For example, if the content of 'LINES.TXT' is as follows:

Once upon a time, there was a woodcutter.


He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf.

Then the function should display output as:


He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.
19.Write a function count_Dwords () in Python to count the words ending with a digit in a text
file "Details.txt".
Example:
If the file content is as follows:
On seat2 VIP1 will sit and
Number of words ending with a digit are 4
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit are 4
BINARY FILES
1. To read the records from a binary file and display the count of the records in the file
2. To get user inputs for rno, name, marks for 5 students from the user and write the same
into a binary file
3. To add a record in the binary file containing the following details : furnitureid, fname, fprice
4. To search for a record in the above binary whose price is above 10000
5. To delete a record from the above binary file whose id is
6. To update the fprice of a furniture searching based on the furnitureid

CSV FILES
1. Write a program using functions:
a) addcustomer() : To write customer code, customer name and amt in the file “cust.csv”
The details of the customers are as follows:
[‘Custcode’,’custname’,’amount’]
[‘001’,’Nihir’,’8000’]
[‘102’,’Akshay’,’5000’]
b) readcustomer(): To read the details of the customers and display them
2. Write a program using following functions:
a) getinventory(): Write code to accept as many inventory records and store them to the csv
file Inventory.csv storing records of inventory as per following structure
Pcode Invname Price Reorder
b) Display(): To display the detail that store in Inventory.csv
3. Write a program code in Python to perform the following using two functions as follows:
a) addbook(): to write to a csv file “book.csv” file bookno, book name and no.of pages with
separator as tab.
b) countrecords: To count and display the total number of records in the “book.csv” file
4. Write Python code to perform the following using two user defined functions.
a) showdata(): To display only rollno and student name of the file “student.csv”
Rollno Name Marks
1 Nilesh 65
2 Akshay 75
b) showselect(): To display only roll number and marks of the students from the csv file
“student.csv”
5. Write a python program using the following functions to :
A file “teacher.csv” contains a city, teacher name and salamount
a) search() : Search and print all rows where city is “Delhi”
Sample “teacher.csv”
City TeacherNAme salamount
Delhi Anil 10000
Pune Dennis 20000
Delhi Das 25000
b) Searchfromfile(): From the file “teacher.csv” print all rows where teacher name is “Anil”
6. Write a Python program using functions:
a) addtransaction() To append bank transactions of following structure to “bank.csv”
TranID Trandate Amount Type
b) getTran(() : To display those transactions whose type is “Deposit”

You might also like