• 巧用OS模块实现python统计代码统计及循环删除文件夹


    一、python统计代码

    方法一(利用迭代思想)、
    import os
    def hand_code_num(file_path,count_num):
        #计数文件代码数量
        with open(file_path, "r", encoding="utf-8") as rf:
            for line in rf:
                #分行判断文件是否满足要求
                if not (line.startswith("#") or line.startswith("
    ")):
                    count_num += 1
        return count_num
    def check_code_number(file_path,count_num=0):
        '''代码计数'''
        #判断是否是py文件
        if os.path.isfile(file_path) and file_path.endswith(".py"):
            count_num=hand_code_num(file_path,count_num)
        else:
            #取得路径下的文件名
            for file_last_name in os.listdir(file_path):
                #对file_path的下的文件名进行拼接,获得新的file_path给变量abs_path
                abs_path=os.path.join(file_path,file_last_name)
                if os.path.isfile(abs_path) and abs_path.endswith(".py"):
                    count_num = hand_code_num(abs_path, count_num)
                elif os.path.isdir(abs_path):
                    #如果新的路径是文件夹就迭代,重复步骤
                    count_num=check_code_number(abs_path,count_num)
        return count_num
    if __name__ == '__main__':
        path="/Users/haiyuanyang/Desktop/python9"
        res = check_code_number(path)
        print(res)
    
    
    方法二(拼接出文件夹下所有的文件路径)、
    #one
    import os
    def count_code_num_func(file_path,count_num=0):
        sepa_info=os.walk(file_path)
        for path_tuple in sepa_info:
            for file_name in path_tuple[2]:
                if file_name.endswith(".py"):
                    com_path=os.path.join(path_tuple[0],file_name)
                    if os.path.isfile(com_path):
                        with open(com_path,"r",encoding="utf-8") as rf:
                            for line in rf:
                                if not (line.startswith("#") or line.startswith("
    ")):
                                    count_num+=1
                                    print(com_path)
        return count_num
    path="/Users/haiyuanyang/Desktop/python9/"
    res = count_code_num_func(path)
    print(res)
    
    #two
    3146
    import os
    import sys
    def count_code_lines(filename):
        res = os.walk(filename)
        count = 0
        for path, _, file_list in res:
            for file in file_list:
                filename = os.path.join(path, file)
                if filename.endswith('py'):
                    with open(filename, 'r', encoding='utf8') as fr:
                        file_count = 0
                        for i in fr:
                            if i.startswith('#') or i.startswith('
    '):
                                continue
                            count += 1
                            file_count += 1
                        print('{}有{}行'.format(filename,file_count))
    
        print('总共有{}行'.format(count))
    
    if __name__ == '__main__':
        # filename = sys.argv[1]
        res=count_code_lines(r'/Users/haiyuanyang/Desktop/python9/')
        print(res)
        count_code_lines(filename)
    

    二、os模块循环删除文件

    import os
    def delete_path(abs_path):
        if os.path.isfile(abs_path):
            os.remove(abs_path)
        else:
            for i in os.listdir(abs_path):
                new_path=os.path.join(abs_path,i)
                if os.path.isfile(new_path):
                    os.remove(new_path)
                else:
                    delete_path(new_path)
        os.rmdir(abs_path)
    if __name__ == '__main__':
        delete_path("/Users/haiyuanyang/Desktop/python8/day171717171")
    
  • 相关阅读:
    hbase-0.92.1过滤器学习
    hbase-0.92.1表备份还原
    hbase-0.92.1集群部署
    hadoop hdfs 数据迁移到其他集群
    Kafka 1.0.0集群增加节点
    Kafka 1.0.0集群安装
    Hadoop 2.7.4 HDFS+YRAN HA删除datanode和nodemanager
    nodemanager 无法启动报错“doesn't satisfy minimum allocations”
    java Collections.sort的使用
    spring RestTemplate提交json格式数据
  • 原文地址:https://www.cnblogs.com/chuwanliu/p/10986604.html
Copyright © 2020-2023  润新知