writing specific lines from one file to another file

user3864194

I'm trying to read a file, look for a specific word and if a line contains that word, remove the line and send the remaining lines to a new file. Here's what I have but it is only finding one of the lines not all of them;

with open('letter.txt') as l:
  for lines in l:
    if not lines.startswith("WOOF"):
      with open('fixed.txt', 'w')as f:
        print(lines.strip(), file=f)
tobias_k

The problem is that when you do with open('fixed.txt', 'w') as f: you basically overwrite the entire content of the file with that one next line. Either open the file in append mode a ...

with open('letter.txt') as l:
    for lines in l:
        if not lines.startswith("WOOF"):
            with open('fixed.txt', 'a') as f:
                print(lines.strip(), file=f)

... or (probably better) open the file in w mode, but just once at the beginning:

with open('letter.txt') as l, open('fixed.txt', 'w') as f:
    for lines in l:
        if not lines.startswith("WOOF"):
            print(lines.strip(), file=f)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy lines containing a specific text from one file to another file

From Dev

Writing from one file to another python

From Dev

Writing from one file to another python

From Dev

How to add lines one by one from one file into another file

From Dev

copying lines from one file to another in python

From Dev

Reading a specific time file and writing the contents to another one

From Dev

Reading from one java file and writing to another one

From Dev

Python:Selecting a range of lines from one file and copying it to another file

From Dev

How to replace/add lines from one file to another file

From Dev

Delete lines from one file if they contain a regex of content in another file

From Dev

Replacing regex matches in one file with lines from another file

From Dev

Changing a specific line in a file from another one

From Dev

Writing special characters from one batch file to another

From Dev

Trouble reading hex from one file and writing to another

From Dev

Trouble reading hex from one file and writing to another

From Dev

Function to identify lines and writing in one file (csv)

From Dev

Removing lines which match with specific pattern from another file

From Dev

Removing lines which match with specific pattern from another file

From Dev

Can i export a specific lines from file to another?

From Dev

What is the simplest way to remove lines from one file matched with lines from another file?

From Dev

Copy specific lines from text file to create a new one

From Dev

Copy 'N' lines from one file to another in python?

From Dev

Move lines matching a pattern from one file to another

From Dev

Copy last n-lines from one file to another

From Dev

Copying lines from one file to another using sed and shell variables

From Dev

How to move N lines from one file to another

From Dev

Python - Writing to a new file from another file

From Dev

Copy selected lines from one file and insert those lines in another file after selected line

From Dev

Writing One File To Another Produces Incorrect Results

Related Related

  1. 1

    Copy lines containing a specific text from one file to another file

  2. 2

    Writing from one file to another python

  3. 3

    Writing from one file to another python

  4. 4

    How to add lines one by one from one file into another file

  5. 5

    copying lines from one file to another in python

  6. 6

    Reading a specific time file and writing the contents to another one

  7. 7

    Reading from one java file and writing to another one

  8. 8

    Python:Selecting a range of lines from one file and copying it to another file

  9. 9

    How to replace/add lines from one file to another file

  10. 10

    Delete lines from one file if they contain a regex of content in another file

  11. 11

    Replacing regex matches in one file with lines from another file

  12. 12

    Changing a specific line in a file from another one

  13. 13

    Writing special characters from one batch file to another

  14. 14

    Trouble reading hex from one file and writing to another

  15. 15

    Trouble reading hex from one file and writing to another

  16. 16

    Function to identify lines and writing in one file (csv)

  17. 17

    Removing lines which match with specific pattern from another file

  18. 18

    Removing lines which match with specific pattern from another file

  19. 19

    Can i export a specific lines from file to another?

  20. 20

    What is the simplest way to remove lines from one file matched with lines from another file?

  21. 21

    Copy specific lines from text file to create a new one

  22. 22

    Copy 'N' lines from one file to another in python?

  23. 23

    Move lines matching a pattern from one file to another

  24. 24

    Copy last n-lines from one file to another

  25. 25

    Copying lines from one file to another using sed and shell variables

  26. 26

    How to move N lines from one file to another

  27. 27

    Python - Writing to a new file from another file

  28. 28

    Copy selected lines from one file and insert those lines in another file after selected line

  29. 29

    Writing One File To Another Produces Incorrect Results

HotTag

Archive