forked from archerslaw/pysam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-lwc-mutil-notext-directory.py
93 lines (82 loc) · 2.87 KB
/
wc-lwc-mutil-notext-directory.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# -*- coding: utf-8 -*-
from optparse import OptionParser
import sys
import os
#print sys.argv
def opt():
parser = OptionParser(usage="usage: %prog [options] [file1 file2 ...]")
parser.add_option("-c", "--char",
dest="characters",
action="store_true",
default=False,
help="Only count characters")
parser.add_option("-w", "--words",
dest="words",
action="store_true",
default=False,
help="Only count words")
parser.add_option("-l", "--lines",
dest="lines",
action="store_true",
default=False,
help="Only count lines")
parser.add_option("-n", "--no-total",
dest="nototal",
action="store_true",
default=False,
help="print total or not")
return parser.parse_args()
#(options, args) = parser.parse_args()
#return (options, args)
#print options,args
def get_count(data):
chars = len(data)
words = len(data.split())
lines = data.count('\n')
return (chars, words, lines)
def print_wc(chars, words, lines, fn):
if options.characters:
print chars,
if options.words:
print words,
if options.lines:
print lines,
print fn
def main():
if args:
t_lines, t_words, t_chars = 0, 0, 0
for fn in args:
if not os.path.exists(fn):
print >>sys.stderr, "%s: No such file or directory" % fn
#sys.stderr.write("%s: No such file or directory\n" %fn)
continue
if os.path.isdir(fn):
sys.stderr.write("%s: Is a directory\n" %fn)
continue
if os.path.isfile(fn):
f = open(fn)
data = f.read()
chars, words, lines = get_count(data)
t_lines += lines
t_words += words
t_chars += chars
print_wc(lines, words, chars, fn)
else:
print >> sys.stderr, "%s: No such file or directory" % fn
if not options.nototal and len(args) > 1:
print_wc(t_lines, t_words, t_chars, 'total')
else:
fn = "stdin"
if path.exists(f):
print '%s: %s: No such file or directory' % (wc-lwc-mutil-no-directory.py, f)
else:
data = sys.stdin.read()
chars, words, lines = get_count(data)
print_wc(chars, words, lines, fn)
if __name__ == "__main__":
#global options, args
(options, args) = opt()
if not (options.characters or options.words or options.lines):
options.characters, options.words, options.lines = True, True, True
main()