472,350 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,350 software developers and data experts.

Delete first line from file

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
Jul 18 '05 #1
4 30323
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

Jul 18 '05 #2
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.*********@durham.ac.uk> 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 . pi****@yellowduck.be . http://www.yellowduck.be/
Jul 18 '05 #3
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

Jul 18 '05 #4
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 (mh******@skippinet.com.au) -
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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Srinivasa T.N. | last post by:
Hi, I want to delete the first 3 lines and last 3 lines of a file. How can I do it? I want to repeat on multiple files.. TIA Regards,...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to...
6
by: santa19992000 | last post by:
I have a file called xyz.txt, if the first word matches, I have to delete the whole line, how can I do that. sample is below file xyz.txt...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output :...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line)....
9
by: Juergen Huber | last post by:
hello, i am a dummy user in python and new in this programming language and so there will be questions, that´s for you no problem! i never have...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r")...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.