功能描述:
在linux命令行运行Python程序WordCount
1.如果只输入单个文件无命令则顺序输出此文件中单词个数、文本行数、字节数。
2.如果输入多个文件无命令则顺序输出这两个文件中单词个数、文本行数、字节数,并且计算每一列的总和total。
3.如1,2所示那样,只不过如果输入带有参数命令的则根据参数命令分别打印相应的文本,命令-w打印单词个数,-l打印文本行数,-c打印文本字节数。
代码如下:
程序名称为WordCount的缩写,wc,不要理解错误。
def wc():
import sys
wcount = 0
lcount = 0
bcount = 0
wtotal = 0
ltotal = 0
btotal = 0
command = sys.argv[1:]
print(command)
for i in sys.argv[1:]:
i = str(i)
#print(command)
if ".txt" in i or ".doc" in i:
try:
file = open(i,'r')
text = file.read()
file.close()
wcount = len(text.split())
lcount = len(text.split("\n"))
bcount = len(text)
wtotal = wtotal+wcount
ltotal = ltotal+lcount
btotal = btotal+bcount
if len(sys.argv[0:])< 100:
if "-c" in command:
#print("-c")
if "-l" in command:
#print("-l")
if "-w" in command:
#print("-w")
print(wcount,lcount,bcount,i)
continue
else:
print(lcount,bcount,i)
continue
else:
print (bcount,i)
continue
elif "-l" not in command and "-w" not in command and "-c" not in command:
print(wcount,lcount,bcount,i)
continue
if "-w" in command:
#print("-w")
if "-l" in command:
#print("-l")
if "-c" in command:
#print("-c")
print(wcount,lcount,bcount,i)
continue
else:
print(lcount,wcount,i)
continue
else:
print(wcount,i)
continue
elif "-l" not in command and "-w" not in command and "-c" not in command:
print(wcount,lcount,bcount,i)
continue
if "-l" in command:
#print("-l")
if "-w" in command:
#print("-w")
if "-c" in command:
#print("-c")
print(wcount,lcount,bcount,i)
continue
else:
print(lcount,wcount,i)
continue
else:
print(lcount)
continue
elif "-l" not in command and "-w" not in command and "-c" not in command:
print(wcount,lcount,bcount,i)
continue
#print(wtotal,ltotal,btotal,"total")
except IOError:
print('wrong file')
if "-c" in command and "-l" in command and "-w" in command and (wtotal != wcount or ltotal != lcount or btotal != bcount ):
print(wtotal, ltotal, btotal, "total")
elif "-c" in command and "-l" in command and "-w" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(ltotal,btotal,"total")
elif "-c" in command and "-w" in command and "-l" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(wtotal,btotal,"total")
elif "-l" in command and "-w" in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(wtotal,ltotal,"total")
elif "-l" in command and "-w" not in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(ltotal,"total")
elif "-l" not in command and "-w" in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(wtotal,"total")
elif "-l" not in command and "-w" not in command and "-c" in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) :
print(bcount,"total")
elif wtotal != wcount or ltotal != lcount or btotal != bcount :
print(wtotal, ltotal, btotal, "total")
wc()
在命令行格式化输入的格式如下:
多个文件:
单个文件:
其中,1.txt , 2.txt , 3.txt 是与.py文件相同路径下的3个文件,测验所用。
1.txt的内容为
one
2.txt的内容为
onetwo
3.txt的内容为
one
two
three
代码简单易懂,注释就没写。