Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 25th, 2004, 11:23 AM
rickt's Avatar
rickt rickt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 rickt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question deleting lines from a file

Ok, I did a search and can't seem to find the exact solution to my problem. In windows, I'm trying to delete specific lines from some files. The line number is static. I can get linecache to display the correct line, but I can't seem to figure out how to delete that line.

I found some code that supposedly deletes the last line of a file. I figured I could tweak that code and use it but it doesn't work for me.

Can someone kick me in the right direction? Thanks!

Reply With Quote
  #2  
Old May 25th, 2004, 11:40 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
read the file line by line and write them to an intermediate file. skip the line you don't want. move the intermediate file to the location your previous file.

Reply With Quote
  #3  
Old May 25th, 2004, 11:55 AM
rickt's Avatar
rickt rickt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 rickt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
readlines() will iterate through the lines, but I can't seem to figure out how to skip the ones I don't want. I had this code:

Code:
fp = open('C:\\filenme.txt', 'rw')
lines = fp.readlines()
del lines[10:13]
fp.close()


But that doesn't really work. I know I'm missing the part where I write the file again and the del lines should be removing lines 10 through 13, but I'm not confident that I wrote that correctly either.

Thanks for the hint though.

Reply With Quote
  #4  
Old May 25th, 2004, 04:10 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Simplistic:
Code:
:
for index, line in enumerate(open('document')):
    if index != 5:
        print >>open('intermediate', 'a'), line,

Reply With Quote
  #5  
Old May 25th, 2004, 10:42 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Another solusion, a mix of the too aproches really.

Code:
#!/usr/bin/env python

lines = file('file.txt', 'r').readlines()
del lines[slice:to:delete]
file('file.txt', 'w').writelines(lines)


Just replace the bold bit with the line number you want to delete - remembering that lists/arrays start with 0 and watch that anoying line go bye bye. But your example should work all you have to do is write the data out with writelines()

The draw back being that this aproch reads the whole file into memory but then Python programs do that every day so don't worry about it .

Have fun,

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : May 25th, 2004 at 10:47 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > deleting lines from a file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap