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

Dani AI

Generated

If you want to remove a specific range of lines without loading the whole file or risking partial writes, stream the file and rewrite everything except the unwanted lines to a temporary file, then atomically replace the original. Line numbers below are 1-based (a common gotcha when coming from list indexing).

import os
import tempfile

def remove_lines_inplace(path, start, end):
    # remove lines start..end (inclusive, 1-based)
    dirpath = os.path.dirname(path) or "."
    fd, tmp = tempfile.mkstemp(dir=dirpath, prefix=".tmp_", text=True)
    try:
        with open(path, "r", encoding="utf-8", newline="") as src, \
             os.fdopen(fd, "w", encoding="utf-8", newline="") as dst:
            for i, line in enumerate(src, start=1):
                if start <= i <= end:
                    continue
                dst.write(line)
        os.replace(tmp, path)  # atomic when on same filesystem
    finally:
        try:
            os.remove(tmp)
        except FileNotFoundError:
            pass

Troubleshooting tip: the error "TypeError: argument 1 must be string ... not list" happens when you pass a list to file.write(). Either iterate and write() each line, or use writelines() or ''.join(list_of_lines). Also note that upper bounds are inclusive in the function above; adjust start/end accordingly. To drop the first N lines, call remove_lines_inplace(path, 1, N).

See: open(), tempfile, and os.replace for details on safe file handling and atomic replacement.

Recommended Answers

All 9 Replies

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\", 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.