954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to remove a number of lines from a text file ?

Hi,

I have jest started learning Python.

I would like to get some help on writing a script that would delete a set number of lines from a text file which looks like this :

1846440556
1846440521
1846440491
1846440505
1846441137
1846441102
1846441080
1846441331
1846441323
1846441315
...
...

Thanks in advance

Cornholio

Cornholio
Newbie Poster
4 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Here is an example how to do this. I had to create your kind of data file first to use it properly ...

data_str = """\
1846440556
1846440521
1846440491
1846440505
1846441137
1846441102
1846441080
1846441331
1846441323
1846441315"""
 
# let's create your data file from the string
fout = open("MyData1.txt", "w")
fout.write(data_str)
fout.close()
 
# read the data file in as a list
fin = open( 'MyData1.txt', "r" )
data_list = fin.readlines()
fin.close()
# test it ...
print data_list
 
print '-'*60
 
# remove list items from index 3 to 5 (inclusive)
del data_list[3:5+1]
# test it ...
print data_list
 
# write the changed data (list) to a file
fout = open("MyData2.txt", "w")
fout.writelines(data_list)
fout.close()
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks fot your help.
I have now modified the script to read the lines from a file. The Python interpreter gives me an error message. What is wrong with the modified code ?
data1 = open('list.txt', "r")
data_str = data1.readlines()
data1.close()
# create your data file from the string
# try 'wb'?
fout = open("MyData1.txt", "w")
fout.write(data_str)
fout.close()

# read the data file in as a list
fin = open( 'MyData1.txt', "r" )
data_list = fin.readlines()
fin.close()
# test it ...
print data_list

print '-'*60

# remove list items from index 3 to 5 (inclusive)
del data_list[3:200+1]
# test it ...
print data_list

# write the changed data (list) to a file
fout = open("MyData2.txt", "w")
fout.writelines(data_list)
fout.close()

Cornholio
Newbie Poster
4 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

What is your error message?

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

the error message is:

File "C:\!cutout\cutOut.py", line 6, in
fout.write(data_str)
TypeError: argument 1 must be string or read-only character buffer, not list

Cornholio
Newbie Poster
4 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Looks like you really butchered vegseat's code. Let me rewrite it a little:

""" assume part of your list1.txt data file looks like this:
1846440556
1846440521
1846440491
1846440505
1846441137
1846441102
1846441080
1846441331
1846441323
1846441315
"""

# read the data file in as a list
fin = open( 'list1.txt', "r" )
data_list = fin.readlines()
fin.close()
# test first 5 list items ...
print data_list[:5]

print '-'*60

# remove list items from index 3 to 5 (inclusive)
del data_list[3:5+1]
# test first 5 list items ...
print data_list[:5]

# write the changed data (list) to a file
fout = open("list2.txt", "w")
fout.writelines(data_list)
fout.close()
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Big Thanks to all of you for help.

bumsfeld, your code does exactly what I wanted.

Cornholio - an absolute beginner

Cornholio
Newbie Poster
4 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

When i try to use this code i still have the data in the orginial filem how do i remove them from the first file?

Godflesh
Light Poster
41 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
When i try to use this code i still have the data in the orginial filem how do i remove them from the first file?

Replacefout = open("list2.txt", "w")
with
fout = open("list1.txt", "w")
to overwrite the original file.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

thanks, offcourse:)

Godflesh
Light Poster
41 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You