Skip to content

Commit 60d49b2

Browse files
Boshra MahmoudiBoshra Mahmoudi
authored andcommitted
implement ls by python
1 parent c6723d0 commit 60d49b2

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

  • implement-shell-tools/ls

‎implement-shell-tools/ls/ls.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import argparse
2+
import os
3+
4+
parser = argparse.ArgumentParser(
5+
prog="ls implemnet by Python"
6+
)
7+
8+
parser.add_argument("-1", action="store_true", dest="one_per_line")
9+
parser.add_argument("-a", action="store_true", dest="show_all")
10+
parser.add_argument("paths", nargs="*")
11+
12+
args = parser.parse_args()
13+
14+
if not args.paths:
15+
args.paths = ["."]
16+
17+
for path in args.paths:
18+
if os.path.isdir(path):
19+
files = sorted(os.listdir(path))
20+
21+
if not args.show_all:
22+
files = [file for file in files if not file.startswith(".")]
23+
24+
if args.one_per_line:
25+
for file in files:
26+
print(file)
27+
else:
28+
for file in files:
29+
print(file, end=" ")
30+
print()
31+
32+
else:
33+
print(path)

0 commit comments

Comments
 (0)