欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python奇偶行分开实现方法

程序员文章站 2023-09-12 23:56:48
# -*- 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("源文件路径","偶行数文件路径","奇行数文件路径")