[Tutor] Copying one text file into another
Alan Gauld
learn2program at gmail.com
Tue Aug 3 19:12:29 EDT 2021
This sounds like it could be homework so I'll suggest a method but not
show you any code.
If it doesn't work show us ypiur code and we will try to help.
On 03/08/2021 19:55, Ed Connell wrote:
> Suppose you have two text files, A.txt and B.txt. You want to copy A into
> B at some designated place .Of course there is copy and paste, but I got
> interested in how you could do this with Python. Can you help?
Assume File A:
A1
A2
A3
A4
And file B
B1
B2
B3
So you want to insert B into A after line A2.
open A.txt for reading
open OUT.txt for writing
for each line in A
write line to OUT.txt.
if line is A2:
open B.txt for reading
for each line in B
write line to OUT.txt
close B.txt
close A.txt
close OUT.txt
rename A.txt to A.bak (or just delete it if you feel brave!)
rename OUT.txt to A.TXT
Try that in Python.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list