|
| 1 | +# Random File Content Picker |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +A Python script that randomly selects and displays lines from a text file. Perfect for displaying random quotes, daily tasks, motivational messages, or any line-based content. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- 📝 Pick one or multiple random lines from any text file |
| 10 | +- 🎲 Each run produces a different random selection |
| 11 | +- 🔢 Optional line numbering for output |
| 12 | +- 🛡️ Comprehensive error handling |
| 13 | +- ⚡ Command-line interface for easy usage |
| 14 | +- 📁 Works with any UTF-8 encoded text file |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +No additional dependencies required! This script uses only Python standard library modules. |
| 19 | + |
| 20 | +```bash |
| 21 | +# Clone the repository |
| 22 | +git clone https://github.com/sumanth-0/100LinesOfPythonCode.git |
| 23 | +cd 100LinesOfPythonCode/snippets/random_file_content_picker |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Basic Usage |
| 29 | + |
| 30 | +Pick a single random line from a file: |
| 31 | + |
| 32 | +```bash |
| 33 | +python random_file_content_picker.py quotes.txt |
| 34 | +``` |
| 35 | + |
| 36 | +### Pick Multiple Lines |
| 37 | + |
| 38 | +Select multiple random lines at once: |
| 39 | + |
| 40 | +```bash |
| 41 | +python random_file_content_picker.py quotes.txt --count 3 |
| 42 | +``` |
| 43 | + |
| 44 | +### Show Line Numbers |
| 45 | + |
| 46 | +Display line numbers with the output: |
| 47 | + |
| 48 | +```bash |
| 49 | +python random_file_content_picker.py quotes.txt --numbered |
| 50 | +``` |
| 51 | + |
| 52 | +### Combined Options |
| 53 | + |
| 54 | +Combine multiple options: |
| 55 | + |
| 56 | +```bash |
| 57 | +python random_file_content_picker.py tasks.txt -c 5 -n |
| 58 | +``` |
| 59 | + |
| 60 | +## Command-Line Arguments |
| 61 | + |
| 62 | +- `filename` (required): Path to the text file to read from |
| 63 | +- `-c, --count`: Number of random lines to pick (default: 1) |
| 64 | +- `-n, --numbered`: Show line numbers with output |
| 65 | +- `-h, --help`: Display help message |
| 66 | + |
| 67 | +## Example |
| 68 | + |
| 69 | +Create a sample quotes file: |
| 70 | + |
| 71 | +```bash |
| 72 | +echo "The only way to do great work is to love what you do. - Steve Jobs" >> quotes.txt |
| 73 | +echo "Innovation distinguishes between a leader and a follower. - Steve Jobs" >> quotes.txt |
| 74 | +echo "Stay hungry, stay foolish. - Steve Jobs" >> quotes.txt |
| 75 | +echo "Life is what happens when you're busy making other plans. - John Lennon" >> quotes.txt |
| 76 | +echo "The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt" >> quotes.txt |
| 77 | +``` |
| 78 | + |
| 79 | +Then run: |
| 80 | + |
| 81 | +```bash |
| 82 | +python random_file_content_picker.py quotes.txt --count 2 --numbered |
| 83 | +``` |
| 84 | + |
| 85 | +Output: |
| 86 | +``` |
| 87 | +============================================================ |
| 88 | +Random pick from: quotes.txt |
| 89 | +============================================================ |
| 90 | +
|
| 91 | +1. Innovation distinguishes between a leader and a follower. - Steve Jobs |
| 92 | +2. Life is what happens when you're busy making other plans. - John Lennon |
| 93 | +
|
| 94 | +============================================================ |
| 95 | +``` |
| 96 | + |
| 97 | +## Use Cases |
| 98 | + |
| 99 | +- 💡 **Daily Motivation**: Display random motivational quotes |
| 100 | +- ✅ **Task Randomizer**: Randomly select tasks from a to-do list |
| 101 | +- 📚 **Learning Tool**: Pick random vocabulary words or study topics |
| 102 | +- 🎮 **Game Ideas**: Select random game prompts or challenges |
| 103 | +- 🍽️ **Meal Planner**: Choose random recipes for the week |
| 104 | +- 💭 **Writing Prompts**: Get random creative writing ideas |
| 105 | + |
| 106 | +## Error Handling |
| 107 | + |
| 108 | +The script handles various error scenarios: |
| 109 | + |
| 110 | +- File not found |
| 111 | +- Permission denied |
| 112 | +- Empty files |
| 113 | +- Invalid encoding |
| 114 | +- Invalid count values |
| 115 | + |
| 116 | +## Requirements |
| 117 | + |
| 118 | +- Python 3.6 or higher |
| 119 | +- No external dependencies |
| 120 | + |
| 121 | +## Contributing |
| 122 | + |
| 123 | +Feel free to submit issues, fork the repository, and create pull requests for any improvements. |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +This project is open source and available under the MIT License. |
| 128 | + |
| 129 | +## Author |
| 130 | + |
| 131 | +Created as part of the 100 Lines of Python Code project. |
| 132 | + |
| 133 | +## Related Issue |
| 134 | + |
| 135 | +This implementation addresses [Issue #790](https://github.com/sumanth-0/100LinesOfPythonCode/issues/790) - Random File Content Picker |
0 commit comments