0% found this document useful (0 votes)
16 views

Practice Exercises Files: Exercise 1

The document contains instructions for two exercises involving reading and writing to files. The first exercise is to open a file, read each line, and prepend it with a line number. The second exercise is to read a file containing animal names, sort the contents alphabetically, and write it to a new output file.

Uploaded by

Karthik
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)
16 views

Practice Exercises Files: Exercise 1

The document contains instructions for two exercises involving reading and writing to files. The first exercise is to open a file, read each line, and prepend it with a line number. The second exercise is to read a file containing animal names, sort the contents alphabetically, and write it to a new output file.

Uploaded by

Karthik
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