Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111170
| From | cs@zip.com.au |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Appending an asterisk to the end of each line |
| Date | 2016-07-06 15:35 +1000 |
| Message-ID | <mailman.119.1467784794.2295.python-list@python.org> (permalink) |
| References | <o-6dnQHLDeVoF-HKnZ2dnUU7-YPNnZ2d@giganews.com> <20160706053513.GA3675@cskk.homeip.net> |
On 05Jul2016 21:37, Python List <python-list@python.org> wrote:
>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.
Put a counter in your loop:
count = 0
for line in f_in.read().splitlines():
f_out.write(line + " *\n")
count += 1
print("count =", count)
Check that it says 200 (or whatever number you expect).
>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')
Importantly for this version, every line _MUST_ have a trailing newline.
Personally that is what I require of my text files anyway, but some dubious
tools (and, IMO, dubious people) make text files with no final newline.
For such a file the above code would eat the last character because we don't
check that a newline is there.
I take a hard line on such files and usually write programs that look like
this:
for line in f_in:
if not line.endswith('\n'):
raise ValueError("missing final newline on file, last line is: %r" (line,))
f_out.write(line[:-1] + ' *\n')
Then one can proceed secure in the knowledge that the data are well formed.
I consider the final newline something of a termination record; without it I
have no faith that the file wasn't rudely truncated somehow. In other words, I
consider a text file to consist of newline-terminated lines, not
newline-separated lines.
Cheers,
Cameron Simpson <cs@zip.com.au>
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:05 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:27 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:29 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:40 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:04 -0400
Re: Appending an asterisk to the end of each line Chris Angelico <rosuav@gmail.com> - 2016-07-06 09:38 +1000
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:49 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 00:03 +0100
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:15 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 19:15 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:29 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:45 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 01:05 +0100
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 20:05 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 03:15 +0100
Re: Appending an asterisk to the end of each line Steven D'Aprano <steve@pearwood.info> - 2016-07-06 12:35 +1000
Re: Appending an asterisk to the end of each line Larry Hudson <orgnut@yahoo.com> - 2016-07-05 21:37 -0700
Re: Appending an asterisk to the end of each line cs@zip.com.au - 2016-07-06 15:35 +1000
csiph-web