Python Delete Line/lines From File Without Modifying Existing Contents
Solution 1:
You can read and write to two different files and do the operation elementwise.
Afterwards you replace the inputfile with the outputfile
import shutil
valid_List = ["10.1.2.3", "10.2.3.4", "10.2.4.5", "10.2.3.7"]
filename = "abc.txt"
outfile = "outfile.txt"withopen(filename, "r") as f:
withopen(outfile, "w") as o:
for line in f:
ifall([line != "%s ok\n" % i for i in valid_List]):
o.write(line)
else:
print("%s Deleted" % line.strip())
shutil.move(outfile, filename)
Caveat This uses the a fixed filename for output, which might cause collisions when you run the program multiple times in parallel. If you use this atomic save recipe you can simplify the code to
valid_List = ["10.1.2.3", "10.2.3.4", "10.2.4.5", "10.2.3.7"]
filename = "abc.txt"with atomic_open(filename, "w") as o:
withopen(filename, "r") as f:
for line in f:
ifall([line != "%s ok\n" % i for i in valid_List]):
o.write(line)
else:
print("%s Deleted" % line.strip())
This will automatically choose a temporary file (collision-free) for you and replace the input file with the output file on completion.
Also you will notice that I have replaced your outer loop (opening files once for each entry in valid_list
) with an all()
statement. This saves you a lot of overhead, too.
Solution 2:
You're opening and closing the huge file multiple times, once for each element in valid_List
. You should instead open the file just once and check if any line of file matches with your valid_List
.
Try like this (the code is not tested but it should work):
valid_List=["10.1.2.3","10.2.3.4","10.2.4.5","10.2.3.7"]
filename="abc.txt"
f = open(filename,"r")
lines = f.readlines()
f.close()
f = open(filename,"w")
for line inlines:
flag = True
deleted = ''for i in valid_List:
if line == i+" "+ "ok"+"\n":
flag = False
deleted = i
breakif flag:
#print("Writing ip not to be deleted")
f.write(line)
else:
print(deleted," Deleted")
f.close()
EDIT Added check for not-found IPs.
valid_List=["10.1.2.3","10.2.3.4","10.2.4.5","10.2.3.7"]
filename="abc.txt"
if_found = [False for v in valid_List]
f = open(filename,"r")
lines = f.readlines()
f.close()
f = open(filename,"w")
for line inlines:
flag = True
deleted = ''for _,i in enumerate(valid_List):
if line == i+" "+ "ok"+"\n":
flag = False
if_found[_] = True
deleted = i
breakif flag:
#print("Writing ip not to be deleted")
f.write(line)
else:
print(deleted," Deleted")
f.close()
for _,i in enumerate(if_found):
ifnot i:
print(valid_List[_]," Not Found")
Solution 3:
i created this script basically you put bunch of lines strings in a list if any of them was found it get deleted and it works on batch so it open multiple files you input the number of files obviously it only for personal use not users because it doesnt have input check and the files need to be in the same dir as the script:
n=int(input('enter the number of files:'))
for i in range (1,n):
f = open(f"{i}.txt","r")
lines = f.readlines()
f.close()
f = open(f"{i}.txt","w")
strings_to_remove=['Edited at','test']
for line inlines:
if line.strip() notin strings_to_remove:
f.write(line)
f.close()
Post a Comment for "Python Delete Line/lines From File Without Modifying Existing Contents"