1. Write a python program to read the first two lines of a file: a.txt as provided in the following output.
The output is:
a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises.
Expected Answer:
Welcome to
www.freelearningpoints.com
2. Write a python program to add text to a file and display the text in python.txt.
The output is:
Expected Answer:
Python Exercises
Python Tutorials
3. Write a python program to read the last n lines of file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
Expected Answer:
www.freelearningpoints.com
4. Write a python program to read the whole file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
Expected Answer:
Welcome to
www.freelearningpoints.com
5. Write a python program to read a file, a.txt line by line.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
['Welcome to\n', 'www.freelearningpoints.com\n', 'You can learn Python tutorials and\n', 'practice with exercises']
6. Write a python program to read a file, a.txt line by line and store it into a variable.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
['Welcome to\n', 'www.freelearningpoints.com\n', 'You can learn Python tutorials and\n', 'practice with exercises']
7. Write a python program to calculate the lines number in a file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
Number of lines in the file: 4
8. Write a python program to identify the longest words in a file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
The longest words is ['www.freelearningpoints.com']
9. Write a python program to calculate the frequency of a all the words in a file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
The total number of each words in the file :
Counter({'Welcome': 1, 'to': 1, 'www.freelearningpoints.com': 1, 'You': 1, 'can': 1, 'learn': 1,
'Python': 1, 'tutorials': 1, 'and': 1, 'practice': 1, 'with': 1, 'exercises': 1})
10. Write a python program to read a file, a.txt line by line and store it into an array..
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
The total number of each words in the file :
['Welcome to\n', 'www.freelearningpoints.com\n', 'You can learn Python tutorials and\n',
'practice with exercises\n']
11. Write a python program to test whether a specified file is exist.
The output is:
file: document.txt:
Expected Answer:
True
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)