文件处理-文本文件相关信息统计

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import os
import sys

def parse_file(path):
    fd = open(path)  #with open(path) as fd:
    i = 0
    spaces = 0
    tabs = 0
    for i,line in enumerate(fd):
        spaces += line.count(' ')
        tabs += line.count('	')
    fd.close()
    return spaces, tabs, i+1
    
def main(path):
    if os.path.exists(path):  #如果path存在
        spaces, tabs, lines = parse_file(path)
        print("spaces{}.tabs{}.lines{}".format(spaces,tabs,lines))
        return True
    else :
        return False

if __name__ == '__main__':
    if len(sys.argv) > 1:
        main(sys.argv[1])  
    else:
        sys.exit(-1)
    sys.exit(0)

 

原文地址:https://www.cnblogs.com/soberkkk/p/12468638.html