从文件的开头和结尾删除行,并将剩余行写入新文件

时间:2018-04-29 22:19:19

标签: python python-3.x list file

我尝试创建一个函数来复制一个文件中的行,从文件中删除第一行omit_from_start和最后omit_from_end行,然后将剩余行写入新文件。

这是我尝试过的:

def truncate_file(file1, file2):
#    file1 = "omit_lines_test.txt"    # Just for testing
#    file2 = "truncated_file.txt"    # Just for testing
    infile = open(file1, "r")
    outfile = open(file2, "w")

    print("\n*** Truncating file copy ***\n")
    omit_from_start = int(input("Omit how many lines from the start: "))
    omit_from_end = int(input("Omit how many lines from the end: "))

    lines_to_output = []

    lines = [line for line in infile]
    lines_to_output.append(str(lines[omit_from_start:omit_from_end]))

    for line in lines_to_output:
        for character in line:
            outfile.write(character)

    infile.close()
    outfile.close()

我的infile只是一个包含['1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '8\n', '9\n', '10\n']的文字文件,我需要outfile来包含['4\n', '5\n', '6\n', '7\n', '8\n'] omit_from_start = 3和{omit_from_end = 2 1}}。

目前,lines_to_output只包含['[]']。我也尝试过使用.join()和.pop()方法,但是他们也不会产生我以后的东西。

1 个答案:

答案 0 :(得分:0)

这种方法确实对infile进行了额外的扫描以找到行数,但它确实具有在复制期间不必将整个infile保持在内存中的好处。因此,对于较小的文件,它可能比原始方法慢,但允许该方法在非常大的文件上工作。

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

def truncate_file(file1, file2):
    infile = open(file1, "r")
    outfile = open(file2, "w")

    print("\n*** Truncating file copy ***\n")
    omit_from_start = int(input("Omit how many lines from the start: "))
    omit_from_end = int(input("Omit how many lines from the end: "))

    length = file_length(file1)

    # This iteration prevents whole file being stored in memory
    for i, line in enumerate(infile):
        if i < omit_from_start:
            continue;
        elif i < length - omit_from_end:
            outfile.write(line)
        elif
           break

    infile.close()
    outfile.close()

实际上没有运行代码,因此可能存在一些边界错误,但方法是扫描infile的文件长度,然后再次遍历内联文件,省略起始行然后中断到达长度时的迭代 - omit_from_end字符串。

没有做任何输入验证以确认omit_from_start介于0和小于length之间减去omit_from_end或者omit_from_end小于length - omit_from_start