File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
implement-shell-tools/implement-shell-tools-python/ls Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ import sys
3+ import os
4+
5+ def list_directory (path , show_all , one_per_line ):
6+ try :
7+ entries = os .listdir (path )
8+ if show_all :
9+ # Add . and .. when -a is used
10+ entries = ["." , ".." ] + entries
11+ else :
12+ entries = [e for e in entries if not e .startswith ("." )]
13+
14+ entries .sort (key = lambda x : (x not in ("." , ".." ), x ))
15+
16+ for e in entries :
17+ print (e )
18+
19+ except Exception as e :
20+ print (f"Error reading directory { path } : { e } " , file = sys .stderr )
21+ sys .exit (1 )
22+
23+
24+ def main ():
25+ args = sys .argv [1 :]
26+
27+ show_all = "-a" in args
28+ one_per_line = "-1" in args
29+
30+ dirs = [a for a in args if a not in ("-a" , "-1" )]
31+
32+ if not dirs :
33+ dirs = [os .getcwd ()]
34+
35+ for i , path in enumerate (dirs ):
36+ if os .path .isdir (path ):
37+ if len (dirs ) > 1 :
38+ print (f"{ path } :" )
39+ list_directory (path , show_all , one_per_line )
40+ if len (dirs ) > 1 and i < len (dirs ) - 1 :
41+ print ("" )
42+ else :
43+ print (path )
44+
45+
46+ if __name__ == "__main__" :
47+ main ()
You can’t perform that action at this time.
0 commit comments