[Tutor] Copying one text file into another

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Aug 3 19:02:26 EDT 2021


On Tue, 3 Aug 2021 13:55:41 -0500, Ed Connell <edwinconnell at gmail.com>
declaimed the following:

>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?
>

	If small, you can load both files into internal lists, split the B-list
at the "designated place", insert A-list at the split, then write the list
to your output file.

	If too long to load into memory, you'll have to open a "b.tmp" (since
you state "copy A into B") for output, open B for input, copy lines (read
line, write line) until you hit the "designated place", open A for input,
copy lines from A to output file, at end resume copying B input to B output
file. Finally, delete B.txt and rename B.tmp to B.txt.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list