Advertisements
Advertisements
Question
Differentiate between readline() and readlines().
Solution
readline() command enables you to read per line in a content and readlines() command is used to read all the content present in a file at the same time and gives list of that item. The readline method extracts a single line from a file and returns it as a string. The readlines method returns a list of strings containing the contents of the entire file, with each object representing one line of the file. With read, you can also review the full file into a substring.
APPEARS IN
RELATED QUESTIONS
Which of the following statements are true?
readlines() method return ______.
Write the use and syntax for the following method:
read()
Which of the following mode in the file opening statement results or generates an error if the file does not exist?
Write a method COUNTLINES() in Python to read lines from the text file ‘TESTFILE.TXT’ and display the lines which do not start with any vowel.
Example:
If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines not starting with any vowel - 1
Write a function ETCount() in Python, which should read each character of a text file “TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T individually (including small cases e and t too).
Example:
If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
E or e : 6
T or t : 9
Write 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.
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 V1P1 will sit and
On seat1 VVIP2 will be sitting
The output will be:
The number of words ending with a digit is 4
Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3)
Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word ‘You’.
Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.