I have two files:
File A:
line A_1
line A_2
line A_3
...
line A_n
File B:
line B_1
line B_2
...
line B_n
line B_n+1
...
line B_m
I want the first n
lines of file A to replace the first n
lines of file B ( m > n
). After this file B should look like this:
File B:
line A_1
...
line A_n
line B_n+1
...
line B_m
What is the most pythonic solution to this?
with open("newB.txt","wb") as fout:
with open("B.txt") as b:
for lineA,lineB in zip(open("A.txt"),b):
fout.write(lineA)
fout.writelines(b)
maybe ... I guess...
with open(A) as fp_A:
for i, line in enumerate(fp_A):
if i <= n:
print(line)
else:
break
with open(B) as fp_B:
for i, line in enumerate(fp_B):
if i > n:
print(line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.