A simple Python command-line tool that mimics the functionality of the Unix wc (word count) command. It can process text from files or standard input, counting bytes, lines, words, and characters. Additionally, it supports real-time monitoring of file changes, allowing you to watch files for modifications and display updated results.
- Word Count: Count the number of words in a file.
- Line Count: Count the number of lines in a file.
- Character Count: Count the number of characters in a file.
- Byte Count: Count the number of bytes in a file.
- Real-time File Monitoring: Watch multiple files for changes and display updated results as files are modified.
- Standard Input (stdin): Process input from stdin if no file is provided.
- Python 3.6 or later
asyncio(standard library in Python 3.6+)word_processing.py(custom module)
-
Clone the repository to your local machine:
git clone https://github.com/7bbg/yourusername/word-processing-cli.git cd word-processing-cli -
Make a virtual environment:
python3 -m venv venv -
Activate virtual environment:
Linux or Mac
source venv/bin/activateWindows
./venv/Scripts/activate
-
Install require packages:
python -m pip install --editable
This project relies on a custom module, word_processing.py, which contains the logic for counting words, lines, bytes, etc. Make sure the word_processing.py file exists and is located in the same directory as your main.py. It should define a class WORD_PROCESSING with a display_results() method.
Run the script from the command line with the desired options. The general syntax is:
python src/main.py [options] [filenames...] -c,--count: Count bytes in the file.-l,--lines: Count lines in the file.-w,--words: Count words in the file.-m,--characters: Count characters in the file.-r,--watch: Monitor files for real-time changes.
python main.py -w example.txtpython main.py -l -w file1.txt file2.txtpython main.py -c -m example.txtMonitor File Changes in Real-Time
python main.py -r file1.txt file2.txtThis command will watch file1.txt and file2.txt for any changes. When a file is modified, the tool will re-display the results.
If no filenames are provided, the program will use stdin as input. You can type the input directly or pipe content to the program.
echo "Hello World" | python main.py -wOr you can simply run the program and input data manually:
python main.py -wPress Ctrl+D to end the input and display the results.
python main.py -rThis command will wait for changes from stdin input, re-processing whenever new data is typed.
- Argument Parser (
argparse): Parses and processes command-line arguments. WORD_PROCESSINGclass: A custom class used to process files or stdin input. It provides methods to count words, lines, characters, and bytes.- Real-Time File Monitoring: Using
asyncio, the script can watch multiple files for changes and re-process them when modified.
watch_file(): Watches a specific file for changes and re-runs the word-processing logic.watch_multiple_files(): Watches multiple files asynchronously for changes.process_files(): Processes files or stdin, depending on user input.main(): The main execution loop, handling user input, file watching, and displaying results.
-
Dynamic Output:
- Allow the tool to update the terminal output dynamically. The terminal should clear and refresh its content as new data is processed, providing a real-time experience to the user.
-
Graceful Handling of Errors:
- If a file becomes inaccessible or is deleted while being monitored, the program should notify the user about the error instead of crashing.
-
Track and Process Only Modified Parts of a File:
- Process the newly modified parts of the file instead of recalculating everything.