0

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?
  • That's why I sometimes don't like the way duplication is handled at SO. The linked question is related but not even close to an answer and now this question won't get any attention and can't be answered. So since I can't chellange the duplication flag, I'll squeeze an answer into a comment and try to make up for not beeing able to format mult line code. – hajef Jun 24 '19 at 13:26
  • To handle this problem, we need to understand Python's iterables and the itertool module. For brevity's sake, I'll just link a [great fundamentals video](https://www.youtube.com/watch?v=EnSu9hHGq5o) and skip to the result: `with open(args*) as file:\n for num, line in enumerate(file):\n print("The line {} of my textFile is: {}".format(num, line))` – hajef Jun 24 '19 at 13:26

3 Answers3

2

You want to loop over the lines of the file. Files provide a very simple interface to do that:

for line in f:
    # Do whatever with each line.

Note that that the lines will include any trailing line break characters.

Also, it's generally best to open files in a with statement:

with open('textFile.txt', 'r') as f:
    for line in f:
        # Do whatever with each line.

This ensures that the file is closed when the with statement finishes, even in the presence of exceptions that might cause explicit close calls to be skipped or lingering references that would cause the file object's finalizer to be delayed.

user2357112 supports Monica
  • 215,440
  • 22
  • 321
  • 400
1
def copy_contents(from_path, to_path):
    from_file = open(from_path, 'r')
    to_file = open(to_path, 'w')

    for no, line in enumerate(from_file.readlines()):
        to_file.write('This is line %u of my textFile: %s' % (no, line))

    from_file.close()
    to_file.close()
Michał Tabor
  • 2,383
  • 5
  • 18
  • 30
1

You can iterate over the file just line by line

with open('textFile1.txt') as f:
    for line in f:
        print line
        # Write to the 2nd file textFile2.txt
sk11
  • 1,669
  • 1
  • 15
  • 27