I've decided to write some Linux commands in Python. Below is a list, along with some constraints (if you're unfamiliar with Linux, the top of the program along has a description about each command and what it does):
ls
: No constraints, just lists all files/directories in current working directory. No flags.cd
: Can only pass..
or another directory. No flags.tree
: No flags or directories can be passed.clear
: No constraints. No flags.whatis
: Only defined commands can be passed. No flags.cat
: Requires a full path to the file. No flags.
I would like feedback on all the functions embodied by the program below, but whatis
in particular. I feel there is a better way than checking each individual function. Any and all recommendations are appreciated.
"""
This is a program to simulate a command line interface
Commands to reimplement:
- ls ( lists all files and directories in current directory )
- cd [directory] ( changes directory )
- tree ( lists all files, directories, sub directories and files from current directory )
- clear ( clears the console screen )
- whatis [command] ( gives a description about the command )
- cat [file] ( outputs the content of the file )
"""
import os
def ls() -> None:
"""
Lists all files and directories in current directory
"""
current_directory = os.getcwd()
for _, directories, files in os.walk(current_directory):
for file in files:
print(file)
for directory in directories:
print(directory)
def cd(directory: str) -> str:
"""
Changes directory
"""
current_directory = os.getcwd()
if directory == "..":
current_directory = current_directory.split("/")[:-1]
current_directory = ''.join(f"{x}/" for x in current_directory)
return current_directory
if os.path.isdir(directory):
return directory
return "Not a directory"
def tree() -> None:
"""
Lists all files, directories, sub directories and files from current directory
"""
current_directory = os.getcwd()
tab_count = 1
print(current_directory.split("/")[-1])
for _, directories, files in os.walk(current_directory):
for file in files:
print("|" + ("-" * tab_count) + file)
for directory in directories:
print("|" + ("-" * tab_count) + directory)
tab_count += 1
def clear() -> None:
"""
Clears the console screen
"""
print("\n" * 100)
def whatis(command: str) -> None:
"""
Prints a description about the passed command
"""
if command == "ls":
print(ls.__doc__)
elif command == "cd":
print(cd.__doc__)
elif command == "tree":
print(tree.__doc__)
elif command == "clear":
print(clear.__doc__)
elif command == "whatis":
print(whatis.__doc__)
elif command == "cat":
print(cat.__doc__)
else:
print("Not a valid command!")
def cat(file_path: str) -> None:
"""
Accepts a path to a file, and outputs the contents of that file
"""
if os.path.exists(file_path):
with open(file_path, "r") as file:
print(''.join(line for line in file))
else:
print("Not a file!")
ls
is in theos
module, you can useos.listdir()
as well ascd
for which you can useod.chdir(path)
and forclear
you may useos.system('cls')
\$\endgroup\$clear
instead ofcls
. \$\endgroup\$cls
is for windows systems, andclear
is for unix systems \$\endgroup\$