I'm in front of a problem in Python. Here is the thing: I have have a text file (textFile1.txt) with several lines example:
This is the line 1
This is the line 2
This is the line 3
I can, in my python script, return all the content of my text file in writing:
def textFile1(self):
my_file = open("textFile1.txt")
my_file_contents = my_file.read()
return my_file_contents
With this function (read()), I return all the content of the file
Now, I would like to write in another text file that I call again with my python program:
The line 1 of my textFile1 is: This is the line 1
The line 2 of my textFile1 is: This is the line 2
The line 3 of my textFile1 is: This is the line 3
But the only thing I'm able to do is to write all the content each time (which is normal because I return all the content of the textFile1.txt) but I don't know how to select just the line 1 of the textFile1.txt, and after the line 2 and after the line 3...
So to summarize, my question is: how to select just ONE line of a text file, and after, to increment it (to print it in the terminal for example) ? I think it's something like:
i=0
f = open("textFile.txt","r")
ligne = f.readline()
print ligne[i]
i=i+1
But in python, I don't know how to do.
Thank you
UPDATE:
Thanks for all your replies but till now, I'm still blocked. By chance, is it possible to select one line from a text file in particular with this function:
for line in f:
print line.rstrip() # display all the lines but can I display just the line 1 or 2?