Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys

# Check if the user used a flag
if len(sys.argv) > 1 and sys.argv[1] == "-n":

# Start from the second argument because the first is "-n"
for filename in sys.argv[2:]:

with open(filename, "r") as file:

line_number = 1

for line in file:
print(f"{line_number} {line}", end="")
line_number += 1


elif len(sys.argv) > 1 and sys.argv[1] == "-b":

# Start from the second argument because the first is "-b"
for filename in sys.argv[2:]:

with open(filename, "r") as file:

line_number = 1

for line in file:

# Only number non-empty lines
if line.strip() == "":
print(line, end="")
else:
print(f"{line_number} {line}", end="")
line_number += 1


else:

# No flag, just print every file
for filename in sys.argv[1:]:

with open(filename, "r") as file:
print(file.read(), end="")
65 changes: 65 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys
import os


# Get arguments from the command line
arguments = sys.argv[1:]


# Check if -1 flag exists
show_one_per_line = False

if "-1" in arguments:
show_one_per_line = True
arguments.remove("-1")


# Check if -a flag exists
show_all_files = False

if "-a" in arguments:
show_all_files = True
arguments.remove("-a")


# If no path is given, use current directory
if len(arguments) == 0:
path = "."
else:
path = arguments[0]


# Check if the path exists
if not os.path.exists(path):
print(f"ls: cannot access '{path}': No such file or directory")
sys.exit()


# If the path is a file, just print the file name
if os.path.isfile(path):
print(path)


# If the path is a folder, list its contents
else:

files = os.listdir(path)


# Remove hidden files unless -a was used
if not show_all_files:
files = [
file for file in files
if not file.startswith(".")
]


# Print one file per line
if show_one_per_line:
for file in files:
print(file)


# Normal ls behaviour
else:
print(" ".join(files))
88 changes: 88 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import sys


# Get everything the user typed after wc.py
arguments = sys.argv[1:]


# These are our options
count_lines = False
count_words = False
count_characters = False


# Store file names here
filenames = []


# Check every argument
for argument in arguments:

if argument == "-l":
count_lines = True

elif argument == "-w":
count_words = True

elif argument == "-c":
count_characters = True

else:
filenames.append(argument)



# If the user did not give any flag,
# show all counts like normal wc
if (not count_lines
and not count_words
and not count_characters):

count_lines = True
count_words = True
count_characters = True



# Go through every file
for filename in filenames:

# Open the file
with open(filename, "r") as file:

# Read the whole file
content = file.read()


# Count lines
lines = len(content.splitlines())


# Count words
words = len(content.split())


# Count characters
characters = len(content)



# Prepare the answer
answer = ""


if count_lines:
answer += str(lines) + " "


if count_words:
answer += str(words) + " "


if count_characters:
answer += str(characters) + " "



# Print the result
print(answer + filename)
Loading