-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.py
More file actions
36 lines (26 loc) · 836 Bytes
/
ls.py
File metadata and controls
36 lines (26 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import argparse
import os
import sys
parser = argparse.ArgumentParser(
prog="ls",
description="List all the files in a directory"
)
parser.add_argument("-a", "--all",action="store_true", help="Include hidden files")
parser.add_argument("-1", "--one", action="store_true",help="One entry per line")
parser.add_argument("dir", nargs="?" ,default=".", help="directory to list")
args = parser.parse_args()
try:
# Gets all file names (including hidden ones)
files = os.listdir(args.dir)
except (FileNotFoundError, NotADirectoryError) as err:
print(f"ls: can't access: '{args.dir}': {err}")
sys.exit(1)
visible_names = [
entry for entry in files
if args.all or not entry.startswith(".")
]
visible_names.sort()
if args.one:
print("\n".join(visible_names))
else:
print("\t".join(visible_names))