Skip to content

Commit 419f1db

Browse files
committed
MyLs in python
1 parent 7ba19f8 commit 419f1db

File tree

1 file changed

+39
-0
lines changed
  • implement-shell-tools/ls

1 file changed

+39
-0
lines changed

implement-shell-tools/ls/ls.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import argparse
2+
import os
3+
4+
def show_unhidden_files(listDir):
5+
for file in listDir:
6+
if not file.startswith('.'):
7+
print(file)
8+
def show_all_files(listDir):
9+
for file in listDir:
10+
print(file)
11+
12+
13+
parser = argparse.ArgumentParser(
14+
prog="my-ls",
15+
description="Simple ls clone with -a and -l options",
16+
)
17+
18+
parser.add_argument("-a", action="store_true", help="include hidden files")
19+
parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format")
20+
parser.add_argument("path", nargs="?", default=".", help="The directory to list")
21+
args = parser.parse_args()
22+
23+
24+
25+
26+
27+
fn = args.path
28+
listDir = os.listdir(fn)
29+
if fn!="":
30+
if args.a and args.one:
31+
show_all_files(listDir)
32+
elif args.a:
33+
show_all_files(listDir)
34+
elif args.one:
35+
show_unhidden_files(listDir)
36+
37+
else:
38+
show_unhidden_files(listDir)
39+

0 commit comments

Comments
 (0)