File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import sys
2+ import os
3+
4+
5+ def main ():
6+ args = sys .argv [1 :]
7+ flags = []
8+ addresses = []
9+ for item in args :
10+ if item [0 ] == "-" :
11+ flags .append (item )
12+ else :
13+ addresses .append (item )
14+
15+ outputLines = []
16+ for fileAddress in addresses :
17+ try :
18+ lines = readFileByLines (fileAddress )
19+ for line in lines :
20+ outputLines .append (line )
21+ except Exception as e :
22+ print (e )
23+ printLines (outputLines , flags )
24+
25+
26+ def readFileByLines (fileAddress ):
27+ file = open (fileAddress , "r" , encoding = "utf-8" )
28+ content = file .read ()
29+ lines = content .split ("\n " )
30+ if len (lines ) != 0 and lines [- 1 ] == "" :
31+ lines .pop ()
32+ return lines
33+
34+
35+ def printLines (outputLines , flags ):
36+ lineNumber = 1
37+ for line in outputLines :
38+ if ("-n" in flags and "-b" not in flags ) or ("-b" in flags and line != "" ):
39+ output = " " + str (lineNumber ) + " " + line
40+ print (output )
41+ lineNumber += 1
42+ else :
43+ print (line )
44+
45+
46+ if __name__ == "__main__" :
47+ main ()
You can’t perform that action at this time.
0 commit comments