LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2012, 08:11 PM   #1
prox
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
How do I remove a particular line in python without erasing the rest of the file?


I want to learn Python so I started writing my first program which is a phone book directory.
It has the options to add a name and phone number, remove numbers, and search for them.
Ive been stuck on the remove part for about 2 days now and just can't get it working correctly. I've been in the Python IRC and everything, but haven't been able to figure it out.
Basically, my program stores the numbers to a list in a file. I cannot figure out how to remove a particular line in the file but keep the rest of the file intact. Can someone please help me with this?

Some people have advised that it will be easier to do if I create a temp file, remove the line, then copy the remaining lines from the original file over to the temp file. Then write over the original file over with the temp file. So I have been trying this...

http://bpaste.net/show/zusmRFYaBsAF09GUcKce/
 
Old 06-22-2012, 12:54 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Read the file into memory, remove the line, and then write it back.

Code:
with open('file.txt') as f:
    lines = f.readlines()
    del lines[3]

with open('file.txt', 'w') as f:
    f.write('\n'.join(lines))
That said, you should really be using SQLite for this. It's part of the standard library, as you can see here:

http://docs.python.org/library/sqlite3.html

Sure you'll need to learn the very basics of SQL, but SQLite will scale much better, and remain fast even when you have hundreds of megabytes worth of phone numbers.

Last edited by dugan; 06-22-2012 at 01:43 PM.
 
Old 06-22-2012, 07:34 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by dugan View Post
when you have hundreds of megabytes worth of phone numbers.
What are you, a phone number pack rat?

I was going to write something about the relative merits of SQLite versus flat files using Python fcntl.lockf() advisory file locking, but fortunately I noticed in time that this is for learning, not an I-need-this-tool case.

For a learning project, I too recommend Python + SQLite. SQLite will certainly come handy in many projects due to its versatility and ease of use.

I would, however, like to add a note of caution, prox. Because SQL tools are so handy, a lot of developers completely rely on them, ignoring all other approaches. Like they say, if your only tool is a hammer, all problems start to look like nails. Take care to learn other approaches, later, too. If for nothing else, then for widening your perspectives.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python, trying to write multiple variables to a file on single line fur Programming 4 02-04-2012 03:38 PM
how to remove a line from a txt file graziano1968 Linux - General 16 11-07-2009 05:17 PM
is there a way to remove files from a CD-RW without erasing it? newbiesforever Linux - General 10 08-08-2007 10:51 PM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 01:07 PM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration