RE: Delete first line from file

2005-03-01 Thread Alex Stapleton
except them memory usage > file size

at least make sure you do it all on disk :P

# i so tested this first, honest
f = open('file', 'r')
fw = open('file.tmp' ,'w')

lc = 0
for l in f:
if lc != 0:
fw.write(l)
else:
lc = 1
f.close()
fw.close()

import os
os.rename('file.tmp', 'file')


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of
Pieter Claerhout
Sent: 01 March 2005 12:51
To: python-list@python.org
Subject: Re: Delete first line from file


what about the following?

f = open( 'file.txt', 'r' )
lines = f.readlines()
f.close()

f = open( 'file.txt'.'w' )
f.write( '\n'.join( lines[1:] ) )
f.close()

cheers,


pieter

On Tue, 1 Mar 2005 12:42:00 +, Peter Nuttall
<[EMAIL PROTECTED]> wrote:
> On Tue, Mar 01, 2005 at 01:27:27PM +0100, Tor Erik S?nvisen wrote:
> > Hi
> >
> > How can I read the first line of a file and then delete this line, so
that
> > line 2 is line 1 on next read?
> >
> > regards
> >
> >
>
> I think you can do something like:
>
> n=false
> f=file.open("") #stuff here
> g=[]
> for line in f.readlines():
>if n: g.append(line)
>n=true
>
> #write g to file
>
> if you are on a unix box, then using the standard untils might be a
> better idea.
>
> Pete
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
pieter claerhout . [EMAIL PROTECTED] . http://www.yellowduck.be/
--
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete first line from file

2005-03-01 Thread Pieter Claerhout
what about the following?

f = open( 'file.txt', 'r' )
lines = f.readlines()
f.close()

f = open( 'file.txt'.'w' )
f.write( '\n'.join( lines[1:] ) )
f.close()

cheers,


pieter

On Tue, 1 Mar 2005 12:42:00 +, Peter Nuttall
<[EMAIL PROTECTED]> wrote:
> On Tue, Mar 01, 2005 at 01:27:27PM +0100, Tor Erik S?nvisen wrote:
> > Hi
> >
> > How can I read the first line of a file and then delete this line, so that
> > line 2 is line 1 on next read?
> >
> > regards
> >
> >
> 
> I think you can do something like:
> 
> n=false
> f=file.open("") #stuff here
> g=[]
> for line in f.readlines():
>if n: g.append(line)
>n=true
> 
> #write g to file
> 
> if you are on a unix box, then using the standard untils might be a
> better idea.
> 
> Pete
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 


-- 
pieter claerhout . [EMAIL PROTECTED] . http://www.yellowduck.be/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete first line from file

2005-03-01 Thread Peter Nuttall
On Tue, Mar 01, 2005 at 01:27:27PM +0100, Tor Erik S?nvisen wrote:
> Hi
> 
> How can I read the first line of a file and then delete this line, so that 
> line 2 is line 1 on next read?
> 
> regards 
> 
>

I think you can do something like:

n=false
f=file.open("") #stuff here
g=[]
for line in f.readlines():
if n: g.append(line)
n=true

#write g to file 

if you are on a unix box, then using the standard untils might be a
better idea. 

Pete

-- 
http://mail.python.org/mailman/listinfo/python-list