File handling in Python
In this section i am going to write few sample programs related to files in Python programming.
I have created a file named with “thefile.txt”. This file contains the following text.
1.Write a python script to read first ‘5’ lines in the file. The file should contain minimum 7 lines.
Output
Hi
everyone This is Sudhakar |
2. Write a python script to read the content from the file and count the number of words in it.
Output
Enter file name: thefile.txt Hieveryone This is Sudhakar Welcome to VMS |
3. To read a character from the user and check the number of occurrences of this character in a given file. For example if the input is ‘s’, check how many number of time ‘s’ is in the file.
Output
Enter file name: thefile.txt Enter letter to be searched:s Occurrences of the letter: 4 |
4.Write a python script to read a file and capitalize the first letter in each word.
Output
Enter file name: thefile.txt HiEveryone This Is Sudhakar Welcome To Vms |
5. Write a python script to read the contents in reverse order.
Output
enter file name:thefile.txt
VMS to Welcome Sudhakar is This everyone Hi |