| | |
Newbie: problems deleting lines from text file (large)
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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!!!
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!!!
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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?

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?
This works just fine for me using a Windows Vista machine:
python Syntax (Toggle Plain Text)
lines = file('./simple-retrieval/file1.txt', 'r').readlines() # show the first 10 lines for a test print lines[:10] del lines[0:4] # now show the first 10 lines for a test print lines[:10] 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.
![]() |
Other Threads in the Python Forum
- Previous Thread: counting collocates -- Help for Linguist, Newbie
- Next Thread: Help with Global Variables - i think
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary dynamic exe file float format function gnu graphics heads homework http ideas import inches input java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer prime programming progressbar py2exe pygame python random recursion redirect schedule scrolledtext software sqlite statictext statistics string strings sudokusolver sum table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






