butterflyTee 0 Light Poster

import sys :o

def main():
usage = 'Usage: %s [options] <file name>\n' % sys.argv[0]
options = 'Options:\n'
options = options + ' -c, --chars print the character counts\n'
options = options + ' -l, --lines print the newline counts\n'
options = options + ' -w, --words print the word counts\n'
usage = usage + options

countChars = 0
countWords = 0
countLines = 0

main()

Dani AI

Generated

Good start, — the outline shows the right ideas (usage text, counters, a main entry). A few focused improvements will make the tool reliable and portable.

Decide exactly what each count means before implementing. “Characters” can mean Unicode code points (decode text, count len(line)) or raw bytes (open in binary, count bytes). “Lines” is usually the number of newline-terminated records; reading line-by-line is the simplest and memory-safe approach. For words, splitting on whitespace with str.split() matches most expectations; use a regex if you need a different definition. Be explicit about encoding when opening files to avoid surprises on non-ASCII input.

Handle large files by streaming (process each line as it is read) instead of slurping the whole file. Support reading from standard input when no filename is given. Use a CLI parser rather than manual argv handling to get automatic help text and clearer flags. Add small unit tests that feed known strings to the counting functions; that makes behavior deterministic and easy to maintain.

For reference on reliable CLI parsing and file handling in modern Python, see the official docs for argparse and open; for word-splitting rules consider the regex guide:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.