Skip to content

Commit 27ffc84

Browse files
committed
completed ls in python
1 parent 68d08db commit 27ffc84

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎implement-shell-tools/ls/ls.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import argparse
2+
import os
3+
from pathlib import Path
4+
5+
# Argument Handling
6+
parser = argparse.ArgumentParser(
7+
prog="ls",
8+
description="Print line, word, and byte counts for each file.",
9+
)
10+
11+
parser.add_argument("-1", action="store_true", help="Show output on separate lines")
12+
parser.add_argument("-a", action="store_true", help="Show hidden files")
13+
parser.add_argument("file_system_items", nargs="*", help="Files or folders to display")
14+
15+
args = parser.parse_args()
16+
17+
# ===== ls Procedure =====
18+
def ls(args):
19+
flag_status = {"print_in_list": False, "show_all": False}
20+
execute_flags(flag_status)
21+
22+
cwd = os.getcwd()
23+
fs_items = check_args_length(args.file_system_items)
24+
dir_args = get_dir_args(cwd, fs_items)
25+
file_args = get_file_args(cwd, fs_items, flag_status)
26+
27+
print_output(populate_output(cwd, fs_items, dir_args, file_args, flag_status), flag_status)
28+
29+
# ===== Flag Handling =====
30+
def execute_flags(flag_status):
31+
if getattr(args, "1"):
32+
flag_status["print_in_list"] = True
33+
if args.a:
34+
flag_status["show_all"] = True
35+
36+
def make_list(output_string):
37+
return output_string.replace("\t", "\n").replace("\n\n", "\n")
38+
39+
# Extracting Data from Arguments
40+
def check_args_length(fs_items):
41+
if len(fs_items) == 0:
42+
fs_items.append(".")
43+
return fs_items
44+
45+
def get_dir_args(cwd, fs_items):
46+
return [
47+
p
48+
for p in fs_items
49+
if (Path(cwd) / p).is_dir()
50+
]
51+
52+
def get_file_args(cwd, fs_items, flag_status):
53+
file_args = [
54+
p
55+
for p in fs_items
56+
if not (Path(cwd) / p).is_dir()
57+
]
58+
if not flag_status["show_all"]:
59+
file_args = remove_dot_files(file_args)
60+
return file_args
61+
62+
# Populating and Outputting Data
63+
def populate_output(cwd, fs_items, dir_args, file_args, flag_status):
64+
output_string = ""
65+
66+
if len(fs_items) == 1:
67+
for file in file_args:
68+
output_string += file + " "
69+
for dir_item in dir_args:
70+
output_string += dir_output(dir_item, cwd, flag_status)
71+
else:
72+
for file in file_args:
73+
output_string += file + "\t"
74+
for dir_item in dir_args:
75+
output_string += f"\n\n{dir_item}:\n" + dir_output(dir_item, cwd, flag_status)
76+
return output_string
77+
78+
def dir_output(dir_item, cwd, flag_status):
79+
target_path = Path(cwd) / dir_item
80+
contents = os.listdir(target_path)
81+
output_str = ""
82+
if flag_status["show_all"]:
83+
output_str += ".\t..\t"
84+
else:
85+
contents = remove_dot_files(contents)
86+
87+
for item in contents:
88+
output_str += item + "\t"
89+
return output_str
90+
91+
def remove_dot_files(file_list):
92+
return [item for item in file_list if not item.startswith(".")]
93+
94+
def print_output(output_string, flag_status):
95+
output = output_string
96+
if flag_status["print_in_list"]:
97+
output = make_list(output)
98+
print(output.rstrip())
99+
100+
# ===== Run ls =====
101+
ls(parser.parse_args())

‎implement-shell-tools/wc/wc.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
parser.add_argument("-l", action="store_true", help="Show line count")
1313
parser.add_argument("-w", action="store_true", help="Show word count")
1414
parser.add_argument("-c", action="store_true", help="Show byte size")
15-
parser.add_argument("files names", nargs="*", help="File names to process")
15+
parser.add_argument("files", nargs="*", help="File names to process")
1616

1717
args = parser.parse_args()
1818

@@ -23,7 +23,7 @@
2323
# ===== wc Procedure =====
2424
def wc(args):
2525
cwd = os.getcwd()
26-
file_names = args.message
26+
file_names = args.files
2727

2828
execute_flags()
2929
all_files_data = add_totals(extract_files_data(file_names, cwd))

0 commit comments

Comments
 (0)