I wrote the following code to help me grab duplicate lines in a file and list out the line number of each duplicated line.
this code works when not in a function. But when I put the code inside a function as is shown below, it's not behaving like I'm expecting it to.
I want the values of the "getallDups" function to be stored in variable data.
#!/usr/bin/env python
filename = '/tmp/test.txt'
f = open(filename, "r")
contentAslist = f.read().splitlines()
def getallDups():
lc = 0
mystring = ""
for eitem in contentAslist:
lc += 1
if contentAslist.count(eitem) > 1:
mystring = lc,eitem
return(mystring)
data = getallDups()
print data
The above code only stores the first duplicated line. it doesn't list all the duplicated lines.
How can this code be modified to do precisely what I want? How can it be modified to store the value of the defined function in the variable "data", which I can then play with.