1 filename='demo.py'
 2 with open(filename,'r')as fp:
 3     lines=fp.readlines() #读取所有行
 4 maxLength=max(map(len,lines))   #最长行的长度
 5 for index,line in enumerate(lines): #遍历所有行
 6     newLine=line.rstrip()   #删除每行右侧的空白字符
 7     newLine=newLine+' '*(maxLength+5-len(newLine))  #在每行固定位置添加行号
 8     newLine=newLine+'#'+str(index+1)+'\n'   #添加行号
 9     lines[index]=newLine
10 with open(filename[:-3]+'_new.py','w')as fp:    #将结果写入文件
11     fp.writelines(lines)

 

相关文章:

  • 2021-12-15
  • 2022-01-29
  • 2021-12-08
  • 2022-01-22
  • 2022-02-25
猜你喜欢
  • 2021-06-25
  • 2022-02-23
  • 2021-11-25
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
相关资源
相似解决方案