Appending an asterisk to the end of each line

Larry Hudson orgnut at yahoo.com
Wed Jul 6 00:37:09 EDT 2016


On 07/05/2016 03:05 PM, Seymore4Head wrote:
> import os
>
> f_in = open('win.txt', 'r')
> f_out = open('win_new.txt', 'w')
>
> for line in f_in.read().splitlines():
>      f_out.write(line + " *\n")
>
> f_in.close()
> f_out.close()
>
> os.rename('win.txt', 'win_old.txt')
> os.rename('win_new.txt', 'win.txt')
>
>
> I just tried to reuse this program that was posted several months ago.
> I am using a text flie that is about 200 lines long and have named it
> win.txt.  The file it creates when I run the program is win_new.txt
> but it's empty.
>
>

Not your problem, but you can simplify your read/write loop to:

for line in f_in:
     f_out.write(line[:-1] + ' *\n')

The 'line[:-1]' expression gives you the line up to but not including the trailing newline.
Alternately, use:  f_out.write(line.rstrip() + ' *\n')

-- 
      -=- Larry -=-



More information about the Python-list mailing list