Assaf Sapir
Assaf Sapir

Reputation: 163

python: copy specific lines from one file to anther file

I have a file, that i want to copy only specific lines from it to anther file. I tried to create a function:

def copytotemp(logfile, fromline, toline):
    with open(logfile) as origfile:
        with open("templog.log", "w") as tempfile:
            for num, line in enumerate(origfile, 0):
                if (num + 1) <= fromline and (num + 1) >= toline:
                    tempfile.write(line)

but tempfile.log is always empty. thanks

Upvotes: 0

Views: 376

Answers (1)

Assaf Sapir
Assaf Sapir

Reputation: 163

i had a mistake with the operators.

def copytotemp(logfile, fromline, toline):
    with open(logfile) as origfile:
        with open("templog.log", "w") as tempfile:
            for num, line in enumerate(origfile, 1):
                if num >= fromline and num <= toline:
                    tempfile.write(line)

is working

Upvotes: 1

Related Questions