File Handling 10: Merge files

# Merge two files

fs1 = open("ipt1.txt", "r")
fs2 = open("ipt2.txt", "r")
fd = open("opt.txt", "w+")

while(True):
Line = fs1.readline()
if not Line:
break
fd.write(Line)

while(True):
Line = fs2.readline()
if not Line:
break
fd.write(Line)

print("Files merged")
print("Displaying new file:")
fd.seek(0,0)
while(True):
Line = fd.readline()
if not Line:
break
print(Line)

fs1.close()
fs2.close()
fd.close()

# Merge two files

fs1 = open("ipt1.txt", "r")
fs2 = open("ipt2.txt", "r")
fd = open("opt.txt", "w+")

L1 = fs1.readlines()
L2 = fs2.readlines()

for Line in L1:
fd.write(Line)
for Line in L2:
fd.write(Line)

print("Files merged")
print("Displaying new file:")
fd.seek(0,0)
L = fd.readlines()
for Line in L:
print(Line)

fs1.close()
fs2.close()
fd.close()

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu