Delete first line from file

Discussion in 'Python' started by Tor Erik Sønvisen, Mar 1, 2005.

  1. 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
     
    Tor Erik Sønvisen, Mar 1, 2005
    #1
    1. Advertising

  2. 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
     
    Peter Nuttall, Mar 1, 2005
    #2
    1. Advertising

  3. 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
    <> 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 . . http://www.yellowduck.be/
     
    Pieter Claerhout, Mar 1, 2005
    #3
  4. Tor Erik Sønvisen

    Jeff Sandys Guest

    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?

    >>> f = open("D:/Pydev/test.txt")
    >>> f.readline()

    'line one\n'
    >>> f.readline()

    'line two\n'
    >>> f.readline()

    'line three\n'


    "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
     
    Jeff Sandys, Mar 1, 2005
    #4
  5. Tor Erik Sønvisen

    Jeff Sandys Guest

    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 seek(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.seek(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 () -
    see 'Help/About PythonWin' for further copyright information.
    >>> f = open("C:/Pydev/test.txt")
    >>> f.readlines()

    ['line one\n', 'line two\n', 'line three\n', 'last line\n']
    >>> ### short four line file
    >>> f.close()
    >>> from myfile import myfile
    >>> f = myfile("C:/Pydev/test.txt")
    >>> f.readline()

    'line one\n'
    >>> f.readline()

    'line two\n'
    >>> f.close()
    >>> ### test, is the file really closed?
    >>> f.readline()

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    ValueError: I/O operation on closed file
    >>> f = myfile("C:/Pydev/test.txt")
    >>> f.readline()

    'line three\n'
    >>> ### reopened file starts where it left off
    >>> f.readline()

    'last line\n'
    >>> f.close()
    >>> f = myfile("C:/Pydev/test.txt")
    >>> f.seek(0)
    >>> ### return to the beginning of the file
    >>> f.readline()

    'line one\n'
    >>>


    This turned out really cool,
    thanks for the good question,
    Jeff Sandys


    "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
     
    Jeff Sandys, Mar 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Stapleton

    RE: Delete first line from file

    Alex Stapleton, Mar 1, 2005, in forum: Python
    Replies:
    0
    Views:
    512
    Alex Stapleton
    Mar 1, 2005
  2. Juergen Huber

    delete first line in a file

    Juergen Huber, Jun 30, 2006, in forum: Python
    Replies:
    10
    Views:
    1,037
    Dennis Lee Bieber
    Jun 30, 2006
  3. Replies:
    6
    Views:
    484
    Richard Heathfield
    Dec 3, 2005
  4. Rob Shepherd
    Replies:
    2
    Views:
    756
  5. Richard Schneeman
    Replies:
    16
    Views:
    521
    Daniel Bush
    Aug 27, 2008
Loading...

Share This Page