File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+ import os
3+
4+ parser = argparse .ArgumentParser (
5+ prog = "ls shell tool" ,
6+ description = "making ls tool with python"
7+ )
8+
9+ parser .add_argument (
10+ "-1" ,
11+ dest = "one_per_line" ,
12+ action = "store_true" ,
13+ help = "lists one per line"
14+ )
15+ parser .add_argument (
16+ "-a" ,
17+ dest = "show_hidden" ,
18+ action = "store_true" ,
19+ help = "show files even hidden files"
20+ )
21+ parser .add_argument (
22+ "items" ,
23+ nargs = "*" , #defualt to current directory
24+ help = "items to list"
25+ )
26+
27+ args = parser .parse_args ()
28+
29+ paths = args .items if args .items else ['.' ]
30+
31+ for path in paths :
32+ try :
33+ entries = os .listdir (path )
34+ if not args .show_hidden :
35+ entries = [e for e in entries if not e .startswith ("." )]
36+ if args .one_per_line :
37+ for entry in entries :
38+ print (entry )
39+ else :
40+ print (" " .join (entries ))
41+ except :
42+ print (f"ls can not access this directory: { path } " )
You can’t perform that action at this time.
0 commit comments