File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+ import os
3+ import sys
4+
5+ parser = argparse .ArgumentParser (
6+ prog = "ls" ,
7+ description = "List all the files in a directory"
8+ )
9+
10+
11+ parser .add_argument ("-a" , "--all" ,action = "store_true" , help = "Include hidden files" )
12+ parser .add_argument ("-1" , "--one" , action = "store_true" ,help = "One entry per line" )
13+ parser .add_argument ("dir" , nargs = "?" ,default = "." , help = "directory to list" )
14+
15+
16+ args = parser .parse_args ()
17+
18+
19+ try :
20+ # Gets all file names (including hidden ones)
21+ files = os .listdir (args .dir )
22+ except (FileNotFoundError , NotADirectoryError ) as err :
23+ print (f"ls: can't access: '{ args .dir } ': { err } " )
24+ sys .exit (1 )
25+
26+ visible_names = [
27+ entry for entry in files
28+ if args .all or not entry .startswith ("." )
29+ ]
30+
31+ visible_names .sort ()
32+
33+ if args .one :
34+ print ("\n " .join (visible_names ))
35+ else :
36+ print ("\t " .join (visible_names ))
You can’t perform that action at this time.
0 commit comments