-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
import argparse | ||
import sys | ||
|
||
def count_lines(filename): | ||
with open(filename, 'r') as file: | ||
line_count = sum(1 for _ in file) | ||
return line_count | ||
|
||
def count_words(filename): | ||
word_count = 0 | ||
with open(filename, 'r') as file: | ||
for line in file: | ||
words = line.split() | ||
word_count += len(words) | ||
return word_count | ||
|
||
def count_chars(filename): | ||
with open(filename, 'rb') as file: | ||
data = file.read() | ||
try: | ||
decoded_data = data.decode('utf-8') | ||
char_count = len(decoded_data) | ||
except UnicodeDecodeError: | ||
char_count = len(data) | ||
return char_count | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(prog="ccwc.py", description="Unix word counter", usage="[program] [file] [options]") | ||
parser.add_argument('filename', help="Input file") | ||
parser.add_argument("-l", "--lines", action='store_true', help="Count the number of lines in the specified file") | ||
parser.add_argument("-w", "--words", action='store_true', help="Count the number of words in the specified file") | ||
parser.add_argument("-m", "--characters", action='store_true', help="Count the number of characters in the specified file") | ||
|
||
args = parser.parse_args() | ||
file = args.filename | ||
|
||
if not(args.lines or args.words or args.characters): | ||
line_count = count_lines(file) | ||
word_count = count_words(file) | ||
char_count = count_chars(file) | ||
print(f"{line_count} {word_count} {char_count} {file}") | ||
def count_lines(lines): | ||
line_count = sum(1 for _ in lines) | ||
return line_count | ||
|
||
if args.lines: | ||
line_count = count_lines(file) | ||
print(f"Number of lines in {file}: {line_count}") | ||
def count_words(lines): | ||
word_count = sum(len(line.split()) for line in lines) | ||
return word_count | ||
|
||
if args.words: | ||
word_count = count_words(file) | ||
print(f"Number of words in {file}: {word_count}") | ||
def count_chars(lines): | ||
char_count = sum(len(line) for line in lines) | ||
return char_count | ||
|
||
if args.characters: | ||
char_count = count_chars(file) | ||
print(f"Number of characters in {file}: {char_count}") | ||
def main(): | ||
parser = argparse.ArgumentParser(prog="ccwc.py", description="Unix word counter", usage="[program] [file] [options]") | ||
parser.add_argument('filename', nargs='?', help="Input file") | ||
|
||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument("-l", "--lines", action='store_true', help="Count the number of lines") | ||
group.add_argument("-w", "--words", action='store_true', help="Count the number of words") | ||
group.add_argument("-m", "--characters", action='store_true', help="Count the number of characters") | ||
|
||
args = parser.parse_args() | ||
|
||
if args.filename: | ||
with open(args.filename, 'r') as file: | ||
lines = file.readlines() | ||
else: | ||
lines = sys.stdin.readlines() | ||
|
||
if not (args.lines or args.words or args.characters): | ||
line_count = count_lines(lines) | ||
word_count = count_words(lines) | ||
char_count = count_chars(lines) | ||
print(f"{line_count} {word_count} {char_count}") | ||
else: | ||
if args.lines: | ||
line_count = count_lines(lines) | ||
print(f"Number of lines: {line_count}") | ||
|
||
if args.words: | ||
word_count = count_words(lines) | ||
print(f"Number of words: {word_count}") | ||
|
||
if args.characters: | ||
char_count = count_chars(lines) | ||
print(f"Number of characters: {char_count}") | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
# cat test.txt | python ccwc.py -l | ||
# python ccwc.py test.txt | ||
# python ccwc.py test.txt -w |