Tags: delete, file, hihow, line, programming, python, thatline

Delete first line from file

On Programmer » Python

5,987 words with 5 Comments; publish: Wed, 30 Apr 2008 15:00:00 GMT; (20093.75, « »)

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

All Comments

Leave a comment...

  • 5 Comments
    • 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

      #1; Wed, 30 Apr 2008 15:01:00 GMT
    • 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 +0000, Peter Nuttall

      <p.s.nuttall.python.todaysummary.com.durham.ac.uk> wrote:

      > On Tue, Mar 01, 2005 at 01:27:27PM +0100, Tor Erik S?nvisen wrote:

      > 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 . pieter.python.todaysummary.com.yellowduck.be . http://www.yellowduck.be/

      #2; Wed, 30 Apr 2008 15:02:00 GMT
    • 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: python-list-bounces+alexs=advfn.com.python.todaysummary.com.python.org

      [mailto:python-list-bounces+alexs=advfn.com.python.todaysummary.com.python.org]On Behalf Of

      Pieter Claerhout

      Sent: 01 March 2005 12:51

      To: python-list.python.todaysummary.com.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 +0000, Peter Nuttall

      <p.s.nuttall.python.todaysummary.com.durham.ac.uk> wrote:

      > On Tue, Mar 01, 2005 at 01:27:27PM +0100, Tor Erik S?nvisen wrote:

      that

      > 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 . pieter.python.todaysummary.com.yellowduck.be . http://www.yellowduck.be/

      --

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

      #3; Wed, 30 Apr 2008 15:03:00 GMT
    • You describe the standard behavior, unless you close the file,

      is that what you want to do; open file, read line 1, close file,

      then open file, read line 2, close file? The other suggestions

      here destory content, do you want that?

      'line one\n'

      'line two\n'

      'line three\n'

      "Tor Erik Snvisen" 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

      #4; Wed, 30 Apr 2008 15:04:00 GMT
    • Here is a non-destructive way keeping track of the

      current file position when closing the file and

      re-open the file where you left off. You can

      always use s(0) to go back to the beginning.

      # -- start of myfile class

      class myfile(file):

      myfiles = {}

      def __init__(self, fname, *args):

      file.__init__(self, fname, *args)

      if self.name in myfile.myfiles:

      pos = myfile.myfiles[fname]

      else:

      pos = 0

      return self.s(pos)

      def close(self):

      myfile.myfiles[self.name] = self.tell()

      file.close(self)

      # -- end of myfile class

      Below is an example with a simple four line file.

      PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]

      on win32.

      Portions Copyright 1994-2001 Mark Hammond (mhammond.python.todaysummary.com.skippinet.com.au) -

      see 'Help/About PythonWin' for further copyright information.

      ['line one\n', 'line two\n', 'line three\n', 'last line\n']

      'line one\n'

      'line two\n'

      Traceback (most recent call last):

      File "<interactive input>", line 1, in ?

      ValueError: I/O operation on closed file

      'line three\n'

      'last line\n'

      'line one\n'

      This turned out really ,

      thanks for the good question,

      Jeff Sandys

      "Tor Erik Snvisen" 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

      #5; Wed, 30 Apr 2008 15:05:00 GMT