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

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()

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()

What is your error message?

the error message is:

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

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()

Big Thanks to all of you for help.

bumsfeld, your code does exactly what I wanted.

Cornholio - an absolute beginner

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?

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?

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

thanks, offcourse:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.