Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107633
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Python path and append |
| Date | 2016-04-25 19:57 -0400 |
| Organization | IISS Elusive Unicorn |
| Message-ID | <mailman.95.1461628677.32212.python-list@python.org> (permalink) |
| References | (3 earlier) <n5qshb5tmq4gk6nvqmad44lb523ouoiji5@4ax.com> <HE1PR07MB1356FDA50BB26CB85681F3E2F0620@HE1PR07MB1356.eurprd07.prod.outlook.com> <mailman.89.1461611345.32212.python-list@python.org> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <o8athblmdphdearvb8ivn7487har012c5q@4ax.com> |
On Mon, 25 Apr 2016 16:15:15 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:
>Thanks for the tip.
>
>Still broke. :(
>
>f = open('wout.txt', 'r+')
>for line in f:
> if line=="":
> exit
> line=line[:-1]
> line=line+" *"
> f.write(line)
> print line
>f.close()
>
>
>
>I did notice that it wrote the 3 lines of test file but it didn't
>append the * after the third entry and it starts printing garbage
>after that.
>
Unless Python (or the C runtime) have implement Xerox CP/V "UPDATE"
file mode*, after the first line is read, the I/O pointer is located at the
end of that line. You now attempt to write the modified line, which will
overwrite parts of the second line (note that you are not writing
new-lines). After that write, you are positioned at some point unknown
(relatively speaking, and do a read operation starting at where the write
ended.
Any I/O operation that changes the length of the data is going to fail,
even if you reposition (look up "seek()" and "tell()")
On Windows, where the physical line-ending is <cr><lf>, but gets
transformed to just <lf> by Python text reads, you are stripping the <lf>,
adding " *". If the next record is the same length, that " *" on write will
replace the <cr><lf> pair.
For the type of operation you are performing, on text files, you really
need to go through the hassle of renames and copies... That is, rename the
input file to some temporary name, open it for read, open the original name
for write, and then read input, modify, write output...
OR -- if the files are short enough, just read the entire file, and
process it in memory...
fupdate = open("theFile.txt", "r+")
fdata = fupdate.readlines()
# doing piecemeal, rather than making a one-liner
for l, ln in enumerate(fdata):
# strip new-line IF present
if ln[-1] == "\n":
ln = ln[:-1]
fdata[l] = ln + " *"
# join new data with newline
fdata = "\n".join(fdata)
#rewind the file
fupdate.seek(0)
fupdate.write(fdata)
fupdate.close()
* UPDATE mode maintained two I/O pointers: a read pointer and write
pointer, and required one to read one or more records before doing a write
operation. As long as the data (record) size did not change it allowed for
Read/Modify/Write of records.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-19 18:29 -0400
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-20 08:38 +1000
Re: Python path and append Matthew Barnett <mrabarnett@mrabarnett.plus.com> - 2016-04-20 00:36 +0100
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 14:10 -0400
Re: Python path and append Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-25 18:24 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 15:00 -0400
RE: Python path and append Joaquin Alzola <Joaquin.Alzola@lebara.com> - 2016-04-25 19:08 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 16:15 -0400
Re: Python path and append Random832 <random832@fastmail.com> - 2016-04-25 16:28 -0400
Re: Python path and append Peter Otten <__peter__@web.de> - 2016-04-25 23:38 +0200
Re: Python path and append Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-25 19:57 -0400
Re: Python path and append Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-25 19:31 +0000
Re: Python path and append MRAB <python@mrabarnett.plus.com> - 2016-04-25 20:44 +0100
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 16:43 -0400
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-26 11:51 +1000
Re: Python path and append Dan Sommers <dan@tombstonezero.net> - 2016-04-26 01:59 +0000
Re: Python path and append Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-26 18:23 +1200
Re: Python path and append boB Stepp <robertvstepp@gmail.com> - 2016-04-29 15:26 -0500
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-30 11:44 +1000
Re: Python path and append John Gordon <gordon@panix.com> - 2016-04-25 21:26 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 18:04 -0400
Re: Python path and append Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-25 20:03 -0400
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-26 11:53 +1000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-26 22:56 -0400
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-27 13:06 +1000
Re: Python path and append Stephen Hansen <me+python@ixokai.io> - 2016-04-27 17:24 -0700
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-26 10:25 +1000
csiph-web