line_max = 0 # 定初值
lines = 1 # 定初值
s = open("demo.py","r").readlines() # 打开并按行读取内容,相当于列表
t = open("demo_new.py","w") # 打开并写入
for i in s: # 循环找出最大行
if line_max < len(i):
line_max = len(i)
for i in s: # 在每行后添加行号
i = i.ljust(line_max+1).replace('\n','') + "#" + str(lines) + "\n"
lines += 1
t.write(i)
行号
最新推荐文章于 2024-07-18 08:30:00 发布