I have an output file (output.txt) which contains a lot of duplicate mac addresses. The output.txt file has around 68 lines at the moment, and I'd say a third are duplicates. I want to remove the duplicates and have managed to do so on the whole with this snippet, placing them in new_lines[] list. However, when reading through the lines of the output.txt file, the for loop seems to miss out reading the last line of the output.txt file. My DEBUG print statement doesn't seem to pick it up and neither does the print inside the for loop.
Have I missed something out of this loop here to pick up the last line of the file?
I'm a bit of a beginner when it comes to Python, so apologies if my formatting of code, or use of code is not great.
Any help appreciated, many thanks!
import os
import time
inputfile = open('paths.txt', 'r')
paths = inputfile.readlines()
outputfile = open('output.txt', 'w')
for line in paths:
linestrip = line.rstrip()
print("\nChecking " + linestrip + " for MAC addresses...\n")
for file in os.listdir(linestrip):
name,ext = os.path.splitext(file)
if file.startswith('00') or file.startswith('805') and ext == ".cfg":
outputfile = open('output.txt', 'a')
outputfile.write(name[6:12] + "\n")
print("Check complete...\n")
time.sleep(1)
print("Starting next path...\n")
time.sleep(1)
print("\nAll paths have been checked.\n")
print("\nRemoving duplicates, please wait...\n")
with open('output.txt', 'r') as rd:
lines = rd.readlines()
new_lines = []
for line in lines:
line = line.rstrip()
print("Checking " + line)
if line not in new_lines:
new_lines.append(line)
print("DEBUG: added " + line + " to list")
time.sleep(1)
print("All duplicates removed\n")
with open('finaloutput.txt', 'w') as rd:
rd.write("\n".join(new_lines))
print("MAC addresses stored in finaloutput.txt")
Small snippet out output.txt file below
45730f
98c2aa
000000
5c5ff2
817a55
a68803
66cee9
6b6844
a688a3
66cc87
f150cf
Small snippet of what I see when I run the code (below)
Checking 98c2aa
DEBUG: added 98c2aa to list
Checking 000000
DEBUG: added 000000 to list
Checking 5c5ff2
Checking 817a55
Checking a68803
Checking 66cee9
Checking 6b6844
Checking a688a3
Checking 66cc87
All duplicates removed
It doesn't seem to check the last one (f150cf).
Aucun commentaire:
Enregistrer un commentaire