File Handling
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
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”