Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Remove duplicate lines from txt file? QUICK

I googled this, seems simple af, but i'm trying some of the code and none of it is working, and I don't really feel like thinking it out.

Can somebody shoot me some code that works. Thanks!

[Image: 9Y60Xjq.png]
http://stackoverflow.com/questions/12152...rom-a-file
Do you need the actual code? Because I know of a website that will remove duplicate lines.
[Image: M6MscUn.gif]

PHP Code

<?php 
def rm_dup_lines
(file_path):
   
with open(file_path) as f:
      
contents = f.readlines()
   for 
line in contents:
      while 
contents.count(line) > 1:
         
contents.remove(line)
   
with open (file_path, "w") as f:
      
f.write("".join(contents)) 

Or simply use a set, sets in Python don't allow duplications:

PHP Code

<?php 
def rm_dup_ lines
(file_path):
   
with open(file_path) as f:
      
contents = f.readlines()
   
result = set(contents)
   
with open(file_path) as f:
      
f.write(''.join(result)) 
Quick and easy way that works perfectly...

PHP Code

<?php 
read 
= open("FILE NAME", 'r').readlines()
read_set = set(read)
new_path = NEW NAME
out 
=  open(new_path, 'w')
for 
line in read_set:
    
out.write(line) 
Well everyone seems like agree on same thing use set function coz its built-in function.
Code
#!/usr/bin/python
import sys

def main(argc, argv):
    print "".join(set(open(argv[1]).readlines())) if argc == 2 else "Usage\n {} filename".format(argv[0])
    return 0

if __name__ == "__main__":
    main(len(sys.argv), sys.argv)
Here's a simple solution - store all of the new lines into a list and use set:

PHP Code

<?php 
f 
= open("file.txt","r") #Open the file for reading
data = f.readlines() #Creates the list for each line
newfile = set(data) #Set removes duplicates in a list
f.close() #Close the file for later use 

Then you can simply write over the old file or create a new one!

PHP Code

<?php 
f 
= open("file.txt","w") #Opens the file for writing - will write over old data
f.write(newfile) #Here we write the information we want
f.close() #Once again - close the file 

There are more simple solutions and one liners, however I want you to understand the process the best you can so I've expanded it for you. 🤝
[Image: 5BgQiIm.gif]

(Apr 22, 2017 07:24 PM)Woes Wrote:  
I googled this, seems simple af, but i'm trying some of the code and none of it is working, and I don't really feel like thinking it out.

Can somebody shoot me some code that works. Thanks!

Hey can you PM me please I have a question about your services
This would be a way to do it with the least amount of code. It also assumes that order is not important.

PHP Code

<?php 
def unique_lines
(filename):
    
with open(filename, "r") as f:
        return list(
map(str.strip, set(f.readlines()))) 

Test file:
Code
this is a test
testing 123
this is a test
testing 123
this is not a test

Code
>>> unique_lines("lol.txt")
['this is not a test', 'testing 123', 'this is a test']
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by MarwenSep 24, 2022 04:19 PM 10 638 Sep 24, 2022 04:19 PM
Last Post: BinaryFox
[Python] by Mr. RobotSep 11, 2022 12:33 AM 1 156 Sep 11, 2022 12:33 AM
Last Post: 570RM
[Shell] by Cyber "Max" ShamanJan 25, 2022 08:56 PM 3 185 Jan 25, 2022 08:56 PM
Last Post: Cyber "Max" Shaman