12345678910111213141516171819#!/usr/bin/env pythonimport os lines = open('/root/install.log', 'r').readlines() new_file = open('/tmp/test_vi.txt', 'w') # 前置几个0呢?# 实际上,可以根据total_lines的大小决定前置几个0的。# total_lines = len(lines)# 这里就不考虑太复杂了,以三个0为例。fmt = '%03d\t%s' line_no = 1for line in lines: new_line = fmt % (line_no, line) new_file.write(new_line) line_no += 1 new_file.close()
温馨提示:答案为网友推荐,仅供参考