Skip to content

Commit 34e629a

Browse files
committed
add very basic error handling
all paths that are not accessible as a file/folder throw same error
1 parent 36c79a6 commit 34e629a

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

  • implement-shell-tools/ls

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,26 @@ def print_entries(entries, onePerLineFlag = args.opt_one):
4444

4545
def main():
4646
# file and directory paths are processed separately
47-
file_args = [arg for arg in args.paths if os.path.isfile(arg)]
48-
dir_args = [arg for arg in args.paths if os.path.isdir(arg)]
47+
# file_args = [arg for arg in args.paths if os.path.isfile(arg)]
48+
# dir_args = [arg for arg in args.paths if os.path.isdir(arg)]
49+
50+
file_args = []
51+
dir_args = []
52+
invalid_args = []
53+
54+
# this is a simplication, it groups all errors under "invalid file"
55+
# real ls would have different messages things like permission denied
56+
# also bad because it makes two syscalls
57+
for arg in args.paths:
58+
if (os.path.isfile(arg)):
59+
file_args.append(arg)
60+
elif (os.path.isdir(arg)):
61+
dir_args.append(arg)
62+
else:
63+
invalid_args.append(arg)
64+
65+
for arg in invalid_args:
66+
print(f"ls: {arg}: No such file or directory", file=sys.stderr)
4967

5068
if (len(file_args) > 0):
5169
print_entries(file_args)

0 commit comments

Comments
 (0)