Python Forum
Do not get how Python iterates over a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do not get how Python iterates over a file
#1
Greetings!
I’m writing Python snippets here and there but I could never understand how Python iterates over a file.
It is kind of a mystery, a black box or something…
Here is an example. I’d like to merge two files.
File1:
1
2
3
File2:
1
2
3
4
5
6
7
And the snippet to merge files.
with open("C:/01/file1.txt", 'r') as f:
    with open("C:/01/file2.txt", 'r') as fe:
        for line_f in f:
            for line_fe in fe:
                if line_f == line_fe:
                    print(f"-- {line_f}")
                else:
                    print(f"++ {line_f}")
I was expecting out up as 1,2,3,4,5,6,7
But I got :
-- 1

++ 2

++ 3

Could anyone help me with this?
I even bought a Python book, it has like 3000000 pages but I still do not get it.
Why I cannot get the right printout?
Thank you.
Reply


Messages In This Thread
Do not get how Python iterates over a file - by tester_V - Jan-27-2023, 06:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Still do not get how Python iterates over a file tester_V 13 3,860 Aug-24-2021, 05:10 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020