python奇偶行分开实现方法
程序员文章站
2022-06-05 14:27:00
# -*- coding: utf-8 -*-
'''
python读取文件,偶数行输出一个文件,奇数行输出一个文件
'''...
# -*- coding: utf-8 -*- ''' python读取文件,偶数行输出一个文件,奇数行输出一个文件 ''' def fenhang(infile,outfile,outfile1): infopen = open(infile,'r',encoding='utf-8') outopen = open(outfile,'w',encoding='utf-8') outopen1 = open(outfile1, 'w', encoding='utf-8') lines = infopen.readlines() i = 0 for line in lines: i += 1 if i % 2 == 0: outopen.write(line) else: outopen1.write(line) infopen.close() outopen.close() fenhang("源文件路径","偶行数文件路径","奇行数文件路径")