-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.py
More file actions
31 lines (26 loc) · 1.02 KB
/
ls.py
File metadata and controls
31 lines (26 loc) · 1.02 KB
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
import os
import argparse
def ls(path, one_column, show_hidden):
"""List files in a directory, optionally in one column or including hidden files."""
try:
files = os.listdir(path)
if not show_hidden:
files = [f for f in files if not f.startswith('.')]
files.sort()
if one_column:
print(*files, sep='\n')
else:
print(*files)
except FileNotFoundError:
print(f"ls: cannot access '{path}': No such file or directory")
except NotADirectoryError:
print(f"ls: cannot access '{path}': Not a directory")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-1', dest='one_column', action='store_true', help='list one file per line')
parser.add_argument('-a', action='store_true', help='show hidden files')
parser.add_argument('path', nargs='?', default='.', help='directory to list')
args = parser.parse_args()
ls(args.path, args.one_column, args.a)
if __name__ == "__main__":
main()