generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathls.py
35 lines (26 loc) · 980 Bytes
/
ls.py
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
import argparse
import os
parser = argparse.ArgumentParser(
prog="The ls program",
description="Implements the ls program"
)
parser.add_argument('dir', nargs='?', type=str, help="Path to the directory to list", default='.')
parser.add_argument("-1", "--one", action="store_true", help="list the directory files one per line")
parser.add_argument("-a", "--hidden_files", action="store_true", help="list the directory files one per line")
args = parser.parse_args()
def list_directory_contents():
is_file_per_line_option = args.one
is_hidden_option = args.hidden_files
dir = args.dir
files = os.listdir(dir)
if is_hidden_option:
files = ['.', '..'] + files
for file in files:
if is_hidden_option or not file.startswith('.'):
if is_file_per_line_option:
print(file)
else:
print(file, end=' ')
if not is_file_per_line_option:
print()
list_directory_contents()