-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrm-duplicates.py
51 lines (43 loc) · 1.39 KB
/
rm-duplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/local/opt/[email protected]/bin/python3
# -*- coding: utf-8 -*-
'''
**rm-duplicates.py**
Remove duplicate lines from textfiles.
Part of "Little PY-Helpers" @ GitHub.com
'''
__author__ = 'DrPython3'
__date__ = '2021-09-19'
__version__ = '1.0'
__contact__ = 'https://github.com/DrPython3'
# [FUNCTION]:
def rm_duplicates(targetfile):
'''
Checks each line in a textfile whether its unique or not.
:param str targetfile: textfile to check
:return: None
'''
already_existing = set()
lines_checked = 0
lines_unique = 0
try:
with open(targetfile, 'r+') as targetcontent:
to_check = targetcontent.readlines()
targetcontent.seek(0)
for line in to_check:
lines_checked += 1
if line.replace('\n', '') not in already_existing:
targetcontent.write(line.replace('\n', ''))
already_existing.add(line.replace('\n', ''))
lines_unique += 1
else:
continue
targetcontent.truncate()
del already_existing
print(f'Lines checked: {str(lines_checked)}')
print(f'Unique lines left: {str(lines_unique)}')
except:
print('Sorry!\nAn error occurred. Check file and try again.')
return None
# [SAMPLE USAGE]:
testfile = input('Testfile, e.g. testfile.txt: ')
rm_duplicates(str(testfile))