"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
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"):
> ..
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
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 !-)
--
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)
>>>
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
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
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.
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
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
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
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
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
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
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
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
16 matches
Mail list logo