This repository was archived by the owner on Feb 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_tools.py
More file actions
69 lines (56 loc) · 2.1 KB
/
text_tools.py
File metadata and controls
69 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
def display_help():
"""Displays a help message."""
help_message = """
Usage: text_tools.py [source_filename] <action>
Actions:
--joiner Joins lines from the source file.
--dupes Removes duplicate lines from the source file.
Options:
source_filename Specify the source file. If omitted, defaults to 'text.txt'.
Examples:
text_tools.py --joiner
text_tools.py source_filename.txt --dupes
text_tools.py --help
Run without arguments to see this help message.
"""
print(help_message)
def join_lines(input_file, output_file):
with open(input_file, 'r') as f:
lines = [line.strip() for line in f.read().splitlines() if line.strip()]
joined_text = ' '.join(lines)
with open(output_file, 'w') as f:
f.write(joined_text)
def remove_duplicate_lines(input_file, output_file):
seen = set()
unique_lines = []
with open(input_file, 'r') as f:
for line in f:
stripped_line = line.strip()
if stripped_line and stripped_line not in seen:
seen.add(stripped_line)
unique_lines.append(line)
with open(output_file, 'w') as f:
f.writelines(unique_lines)
if __name__ == '__main__':
# Default source file and action
source_file = 'text.txt'
destination_file = 'output.txt'
action = None # this will be either "joiner" or "dupes"
if len(sys.argv) == 1 or '--help' in sys.argv:
display_help()
# Parse arguments
for arg in sys.argv[1:]:
if arg in ['--joiner', '--dupes']:
action = arg[2:]
else:
source_file = arg
# Determine the action to take based on the flag
if action == "joiner":
join_lines(source_file, destination_file)
print(f"Lines from {source_file} have been joined and saved to {destination_file}.")
elif action == "dupes":
remove_duplicate_lines(source_file, destination_file)
print(f"Duplicate lines from {source_file} have been removed and saved to {destination_file}.")
else:
print("Please specify a valid action using either --joiner or --dupes.")