[Tutor] Copying one text file into another

Alan Gauld alan.gauld at yahoo.co.uk
Sat Aug 7 08:03:41 EDT 2021


On 05/08/2021 21:47, Ed Connell wrote:
> Thanks for your suggestions.  Though I can see why you thought of HOMEWORK,
> that is funny since I will turn 80 in a few days. :)

Even 80 year olds can be doing homework! :-)

> Here the destination is a temp file as was suggested by someone.
> 
> def insert_file( dest, lookfor, filea, fileb ):
>     with open( dest, 'w' ) as fout:
>         with open( filea, 'r' ) as fin:
>             while True:
>                 lin = fin.readline()
>                 print( lin[:-1], file = fout )

I'm not sure why you are using print() to write to the file. It
will work, but it is not expressing clearly the purpose of your
code, namely to write to the file. It would be clearer (and
hence more maintainable, long term) to use fout.write()

[ Bonus points....
Alternatively, just use print() but not to a file. That will
display the output on stdout. You can then redirect stdout
to a file when you run the program:

C:\> python insertfile.py file1.txt file2.txt XXX  > results.txt

Of course you'd need to write some code to read the arguments
from sys.argv when you run the code.

This makes your program work like many of the Unix/Linux OS
utilities and is easy to debug/test. But it does require a
wee bit extra work collecting the arguments from the command
line.

For bonus points you could even have it read from stdin so
that you only specify the marker and the file to insert
on the command line. You would then use input
(and a while loop!) to read the input and print to display
the output. To make it use files you would type:

C:\> python insertfileat infile.txt XXX <filein.txt >fileout.txt

Just for some more fun! :-)
...]

>                 if len( lin ) == 0:
>                     print( '\n\n\n', file = fout )
>                     return

If you used a for loop this block would go in the else block
since it is executed when you reach the end of the file:

for line in filea:
     process the line
else:   #only executed if you reach the end of file.
    process end of file.

>                 if lookfor in lin:
>                     break

You don't really need the break or the three while loops.
If you use a for loop to iterate over filea that will
guarantee to process all of the file. And you can
just insert the processing of the second file at this point.

>             with open( fileb, 'r' ) as fin2:
>                 while True:
>                     lin = fin2.readline()
>                     print( lin[:-1], file = fout )
>                     if len( lin ) == 0:
>                         print( '\n\n\n', file = fout )
>                         break

Again a for loop will do the job using the same pattern
as above.

>             while True:
>                 lin = fin.readline()
>                 print( lin[:-1], file = fout )
>                 if len( lin ) == 0:
>                     print( '\n\n\n', file = fout )
>                     return

And the for loop above would remove the need to duplicate
the end of file processing.

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