Skip to content

Commit

Permalink
Resolves for file is piped from std
Browse files Browse the repository at this point in the history
  • Loading branch information
ASproson committed May 6, 2024
1 parent 9ca14df commit 33830d1
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions ccwc.py
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

0 comments on commit 33830d1

Please sign in to comment.