python_文件读写_反转文件中的行
简要说明
功能:反转文件中的行
输入:一个文件.txt
1.png
2.png
3.png
输出:一个文件.txt
3.png
2.png
1.png
代码示例
import os
import sys
def revese_lines_in_file(input_file_name, output_file_name):
input_file = open(input_file_name, 'r')
output_file = open(output_file_name, 'w')
content_list = input_file.readlines()
contentall = [x.strip() for x in content_list]
contentall_new = contentall[::-1]
for x in contentall_new:
output_file.write(x +"\n")
input_file.close()
output_file.close()
if __name__ == '__main__':
print("usage: python revers_img_seq.py input_file_name output_file_name")
try:
input_file_name, output_file_name = sys.argv[1:3]
print("input_file_name: {0}\noutput_file_name: {1}\n".format(input_file_name,output_file_name))
revese_lines_in_file(input_file_name, output_file_name)
print("Done.")
except Exception as e:
print(sys.argv)
print(e)
还没有评论,来说两句吧...