Newbie: problems deleting lines from text file (large)

Thread Solved

Join Date: Apr 2008
Posts: 2
Reputation: eemqjunk is an unknown quantity at this point 
Solved Threads: 0
eemqjunk eemqjunk is offline Offline
Newbie Poster

Newbie: problems deleting lines from text file (large)

 
0
  #1
Apr 25th, 2008
hello,

i'm a newbie programmer (and to python, double trouble and running into issues reading and writing files. i'm successfully running the code below to manipulate and remove the first few lines of a text file. The text file (file1.txt) is approx. 329kb but when i run the code it only writes a new 5kb file (file2.txt)... so the other 300 something kb below that is missing. how can i get the full file (to file2.txt) with only the first 4 lines removed???

so confused... please help!!

lines = file('./simple-retrieval/file1.txt', 'r').readlines()
del lines[0:4]
file('./simpleunwrapped/file2.txt', 'w').writelines(lines)

Thanks in advance!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Newbie: problems deleting lines from text file (large)

 
0
  #2
Apr 25th, 2008
Replace del lines[0:4] with lines = lines[4:]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: eemqjunk is an unknown quantity at this point 
Solved Threads: 0
eemqjunk eemqjunk is offline Offline
Newbie Poster

Re: Newbie: problems deleting lines from text file (large)

 
0
  #3
Apr 25th, 2008
I get the same exact results

here's another thing i should throw in... if i comment out the "del lines" line the results are the same... the whole file is still not copying to the new location.

it looks like the whole file never really opens...

As far as opening the file... am i doing it correctly?? i had an impression that readlines() as opposed to readline() is suppose to read the whole file into memory... is this true?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Newbie: problems deleting lines from text file (large)

 
0
  #4
Apr 26th, 2008
This works just fine for me using a Windows Vista machine:
  1. lines = file('./simple-retrieval/file1.txt', 'r').readlines()
  2.  
  3. # show the first 10 lines for a test
  4. print lines[:10]
  5.  
  6. del lines[0:4]
  7.  
  8. # now show the first 10 lines for a test
  9. print lines[:10]
  10.  
  11. file('./simpleunwrapped/file2.txt', 'w').writelines(lines)
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC