0% found this document useful (0 votes)
13 views2 pages

PracticeExercises Files

Uploaded by

Nandu prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

PracticeExercises Files

Uploaded by

Nandu prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practice Exercises ­ Files

Exercise 1:

Create a program that opens file.txt. Read each line of the file and prepend it with a line
number.

The contents of files.txt:

This is line one.


This is line two.
Finally, we are on the third and last line of the file.

Sample output:

1: This is line one.


2: This is line two.
3: Finally, we are on the third and last line of the file.

Exercise 2:

Read the contents of animals.txt and produce a file named animals­sorted.txt that is sorted
alphabetically.

The contents of animals.txt:

man
bear
pig
cow
duck
horse
dog

LinuxTrainingAcademy.com
After the program is executed the contents of animals­sorted.txt should be:

bear
cow
dog
duck
horse
man
pig

LinuxTrainingAcademy.com

You might also like