python写一个计算 代码行数 的算法

import os

path="D:\\programs\\python\\test"
ex=["common"]
ext=[".html",".htm",".py",".js"]
def calc(file):
    f=open(file,mode='r',encoding='utf-8')
    c=f.readlines()
    f.close()
    return len(c)
def list_dir(ph,ex,data):
    for f in os.listdir(ph):
        f=os.path.join(ph,f)
        if os.path.isfile(f) and os.path.splitext(f)[1] in ext:
            data.append(f)
        if os.path.isdir(f):
            if os.path.dirname(f).split('\\')[-1] not in ex:
                list_dir(f,ex,data)
data=[]
list_dir(path,ex,data)
s=0
for d in data:
    l=calc(d)
    print("%s : %d " % (d,l))
    s+=l
print("总行数:%d" % s)

非常简单,指定一下目录、排除目录、文件扩展名即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux下使用Python来类似于Word计算页数和每页行数算法,可以通过以下步骤来实现: 1. 首先,将文本内容按照指定的字体、字号、行间距等格式进行分析和计算。可以使用Python中的文本处理库,如`python-docx`来读取Word文档内容。 2. 确定每页的页眉和页脚高度,以及页面的上下边距。这些参数将对每页行数计算产生影响。 3. 计算每行的高度。根据指定的字体和字号,以及行间距,确定每行的高度。 4. 统计每一页的行数。将每行的高度,页面的上下边距以及页眉和页脚高度作为约束条件,来判断每一页能够容纳的行数。 5. 统计总页数。将文本的总行数除以每一页的行数,并向上取整,即可得到总页数。 下面是一个简单的示例代码,演示了如何通过使用Python和`python-docx`库计算文档的总页数和每页行数: ```python from docx import Document from math import ceil def calculate_page_numbers(filepath, font_size, line_spacing, page_margin, header_height, footer_height): doc = Document(filepath) total_lines = 0 for paragraph in doc.paragraphs: # 统计每段文字的行数 lines = ceil(len(paragraph.text) / page_width) + 1 total_lines += lines # 计算每页的行数 page_height = page_margin['top'] + page_margin['bottom'] + header_height + footer_height lines_per_page = ceil((page_height - header_height - footer_height) / line_height) # 计算总页数 total_pages = ceil(total_lines / lines_per_page) return total_pages, lines_per_page # 设置字体、字号、行间距等参数 font_size = 12 line_spacing = 1.2 page_margin = {'top': 1, 'bottom': 1} header_height = 2 footer_height = 2 # 文档路径 filepath = 'path/to/your/document.docx' # 调用函数计算总页数和每页行数 total_pages, lines_per_page = calculate_page_numbers(filepath, font_size, line_spacing, page_margin, header_height, footer_height) print('总页数:', total_pages) print('每页行数:', lines_per_page) ``` 以上代码仅提供了一个简单的实现思路,具体的计算方式还需要根据实际需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值