Tutorial 4 - Python - / Manage the content of files/


Exercise 1

Consider that you have a text file containing lines of different lengths (you can create this file using a text editor like NotePad and typing what you want inside).

Write a script that searches and displays the longest line.



Exercise 2

From 3 pre-existing text files A.txt, B.txt, C.txt (you can create these files yourself with BlocNote for ie. by putting a few sentences into them and then saving them in .txt format), create a new D.txt file that contains all the content of the 3 files.



Exercice 3

Suppose you have a text file containing names as the first column, amount of money in bank as second column, and interests rate (in%) as the third column, separated by a tabulation (« \t »), ig.:



Jean 3000 1.2

Bob 2500 1.4

Fred 2200 2.5



Write a script that displays the amount of money for each people after 10 years, considering the interests rate.



Some help :

* convert a string to a list :

L = my_string.split(« \t ») # here the separator is a tabulation (character « \t »)

* list to string (should not be needed, for this exercise) :

my_string = «\t ».join(L) # here the separator is a tabulation



More :

- The previous question (compute and display names + new amounts) should be coded as a function. Reuse this function to write the results into a text file.