zoukankan      html  css  js  c++  java
  • 文件处理-文本文件相关信息统计

    #!/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)

     

  • 相关阅读:
    isalnum()方法
    index()方法
    find()方法
    expandtabs()方法
    endswith()方法
    encode()方法
    bytes.decode()方法
    count()方法
    center()方法
    capitalize()方法
  • 原文地址:https://www.cnblogs.com/soberkkk/p/12468638.html
Copyright © 2011-2022 走看看