Skip to content

Commit c64c5dd

Browse files
committed
Complete ls.py
1 parent 3e13fb5 commit c64c5dd

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
import argparse
3+
import os
4+
5+
parser = argparse.ArgumentParser(
6+
prog="ls",
7+
description="List all files name",
8+
)
9+
10+
parser.add_argument("-1", dest="one", action="store_true", help="A new line for each file")
11+
parser.add_argument("-a", action="store_true", help="Show hidden files")
12+
parser.add_argument("path", nargs="?", default=".", help="The directory to list")
13+
14+
args = parser.parse_args()
15+
path = args.path
16+
17+
try:
18+
files = os.listdir(path)
19+
if not args.a:
20+
files = [file for file in files if not file.startswith(".")]
21+
if args.one:
22+
print("\n".join(files))
23+
else:
24+
print(" ".join(files))
25+
26+
except Exception as e:
27+
print(f"Error reading {path}: {e}")

0 commit comments

Comments
 (0)