-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path去重可排序.py
70 lines (57 loc) · 1.48 KB
/
去重可排序.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Forsaken
import getopt
import os
import sys
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'hf:s', ['help', 'file=', 'sort'])
except getopt.GetoptError as e:
print('[-] %s' % e)
usage()
sys.exit(2)
file = ''
sort = False
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-f', '--file'):
file = a
elif o in ('-s', '--sort'):
sort = True
else:
pass
if not file:
print('[-] File Arguments Not Found!')
usage()
sys.exit(2)
if not os.path.exists(file):
print('[-] File Not Found!')
sys.exit(1)
with open(file, 'r') as f:
old = f.readlines()
old_len = len(old)
new = list()
for o in old:
if not o in new:
new.append(o)
new_len = len(new)
delete = old_len - new_len
if sort:
new.sort()
out = 'new_' + file
with open(out, 'w') as f:
f.writelines(new)
print('Delete %s Line' % delete)
print('Please Check %s' % out)
def usage():
print('Usage: python %s [options]' % sys.argv[0])
print('')
print('Options:')
print(' -h, --help Show Help Message And Exit')
print(' -f FILE, --file=FILE File')
print(' -s, --sort Sort')
if __name__ == '__main__':
main()