Python Exercise Lists

Python File Input Output Exercises

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

Click for the answer



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

Click for the answer



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

Click for the answer



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

Click for the answer



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']

Click for the answer



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']

Click for the answer



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

Click for the answer



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']

Click for the answer



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})

Click for the answer



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']

Click for the answer



11. Write a python program to test whether a specified file is exist.

The output is:

file: document.txt:

Expected Answer:
True

Click for the answer



                               


More Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises
Python Lambda Exercises
Python Function Exercises
Python File Input Output Exercises
Python Tkinter Exercises


More Numpy Exercises:

Numpy String Exercises
Numpy Integer Exercises
Numpy Array Exercises
Numpy Data Analysis Exercises
Numpy Data Sorting Exercises


More Pandas Exercises:

Pandas Series Exercises


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression