Re: Writing to a certain line?

2006-06-08 Thread John Savage
"Tommy B" <[EMAIL PROTECTED]> writes: >I was wondering if there was a way to take a txt file and, while >keeping most of it, replace only one line. See, I'd have a file like: .. >Is there any easy way to do this? An easy way? If you know how many bytes in from the start of the file your desired

Re: Writing to a certain line?

2006-06-07 Thread Jack Diederich
On Wed, Jun 07, 2006 at 08:17:22PM +0200, Fredrik Lundh wrote: > Christophe wrote: > > > Use /dev/zero as source and /dev/null as destination :D > > have you tried looping over an endless null-byte stream? > > on a random Linux server, this statement > > >>> for line in open("/dev/zero"): > ..

Re: Writing to a certain line?

2006-06-07 Thread Fredrik Lundh
Christophe wrote: > Use /dev/zero as source and /dev/null as destination :D have you tried looping over an endless null-byte stream? on a random Linux server, this statement >>> for line in open("/dev/zero"): ... print len(line) ... terminates without printing anything after about three s

Re: Writing to a certain line?

2006-06-07 Thread bruno at modulix
Christophe wrote: > bruno at modulix a écrit : (snip) >> Wrong guess - unless, as Fredrik suggested, you have an infinite disk >> with an infinite file on it. If so, please share with, we would be >> *very* interested !-) > > > Use /dev/zero as source and /dev/null as destination :D Lol !-) --

Re: Writing to a certain line?

2006-06-07 Thread Christophe
bruno at modulix a écrit : > Tommy B wrote: > >>bruno at modulix wrote: > > > (snip) > > >>>import os >>>old = open("/path/to/file.txt", "r") >>>new = open("/path/to/new.txt", "w") >>>for line in old: >>> if line.strip() == "Bob 62" >>> line = line.replace("62", "66") >>> new.write(line) >>>

Re: Writing to a certain line?

2006-06-07 Thread bruno at modulix
Tommy B wrote: > bruno at modulix wrote: (snip) >>import os >>old = open("/path/to/file.txt", "r") >>new = open("/path/to/new.txt", "w") >>for line in old: >> if line.strip() == "Bob 62" >>line = line.replace("62", "66") >> new.write(line) >>old.close() >>new.close() >>os.rename("/path/to/n

Re: Writing to a certain line?

2006-06-06 Thread John Machin
On 7/06/2006 7:46 AM, Tommy B wrote: > > Umm... I tried using this method and it froze. Infiinite loop, I'm > guessing. > Don't guess. Instead: (1) Put some print statements into your code to show what is happening: (a) before start of loop (b) one or more salient points inside loop (c) at (e

Re: Writing to a certain line?

2006-06-06 Thread Fredrik Lundh
Tommy B wrote: >> import os >> old = open("/path/to/file.txt", "r") >> new = open("/path/to/new.txt", "w") >> for line in old: >> if line.strip() == "Bob 62" >> line = line.replace("62", "66") >> new.write(line) >> old.close() >> new.close() >> os.rename("/path/to/new.txt", "/path/to/file.

Re: Writing to a certain line?

2006-06-06 Thread Tommy B
bruno at modulix wrote: > Tommy B wrote: > > I was wondering if there was a way to take a txt file and, while > > keeping most of it, replace only one line. > > > This is a FAQ (while I don't know if it's in the FAQ !-), and is in no > way a Python problem. FWIW, this is also CS101... > > > You

Re: Writing to a certain line?

2006-06-06 Thread gene tani
bruno at modulix wrote: > > Else - if you want/need to stick to human readable flat text files - at > least write a solid librairy handling this, so you can keep client code > free of technical cruft. > > HTH > -- > bruno desthuilliers for human readable, you might want to look at reading and wri

Re: Writing to a certain line?

2006-06-06 Thread bruno at modulix
Rene Pijlman wrote: > bruno at modulix: > >>You can't do this in place with a text file (would be possible with a >>fixed-length binary format). > > > More precise: it's possible with any fixed-length change, in both binary > and text files, with both fixed and variable formats. > Granted. But

Re: Writing to a certain line?

2006-06-06 Thread Rene Pijlman
bruno at modulix: >You can't do this in place with a text file (would be possible with a >fixed-length binary format). More precise: it's possible with any fixed-length change, in both binary and text files, with both fixed and variable formats. -- René Pijlman -- http://mail.python.org/mailman

Re: Writing to a certain line?

2006-06-06 Thread Fredrik Lundh
bruno at modulix wrote: > > This is a FAQ (while I don't know if it's in the FAQ !-), and is in no > way a Python problem. FWIW, this is also CS101... > feel free to repost your response here: http://pyfaq.infogami.com/suggest -- http://mail.python.org/mailman/listinfo/python-list

Re: Writing to a certain line?

2006-06-06 Thread bruno at modulix
Tommy B wrote: > I was wondering if there was a way to take a txt file and, while > keeping most of it, replace only one line. This is a FAQ (while I don't know if it's in the FAQ !-), and is in no way a Python problem. FWIW, this is also CS101... You can't do this in place with a text file (wo

Re: Writing to a certain line?

2006-06-06 Thread Rene Pijlman
Tommy B: >I was wondering if there was a way to take a txt file and, while >keeping most of it, replace only one line. You'd need to read the file and parse it, to find the start position of the line you want to change. Then seek output to that position and write and flush the changes. You must k

Writing to a certain line?

2006-06-05 Thread Tommy B
I was wondering if there was a way to take a txt file and, while keeping most of it, replace only one line. See, I'd have a file like: Tommy 555 Bob 62 Joe 529 And I'd want to set it to be: Tommy 555 Bob 66 Joe 529 Is there any easy way to do this? -- http://mail.python.org/mailman/listinfo/p