Python Forum
Delete Lines that Contain Words - Loop through files in a folder - Write to new files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete Lines that Contain Words - Loop through files in a folder - Write to new files
#1
Hi friends,

I am trying to loop through files and from each file delete the lines that contain the search keywords




import os

def remove_line_from_file(filename, line_to_remove, dirpath=''):
  
    filename = os.path.join(dirpath, filename)
    temp_path = os.path.join(dirpath, 'temp.txt')


    
    with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:


        search_keywords=['Car','Train']                # Delete all sentences that contain these words
        
        for line in f_read:
            if (any(map(lambda word: word in line, search_keywords))):
                    
                if line.strip() == word:            #f line.strip() == line_to_remove:
                    continue
                temp.write(line)

        os.remove(filename)
        os.rename(temp_path, filename)
        directory = 'C:/Users/Home/Desktop/test/'
        dirpath, _, files = next(os.walk(directory))

        for f in files:
            remove_line_from_file(f, line, dirpath)
I dont get an error as such - but I cant work out why it doesnt work.

It may not be syntactically correct as i borrowed code from here and there.

Please may a kind person have a look at this

thank you for your time and help



:)


Python newbie trying to learn the ropes
Reply


Messages In This Thread
Delete Lines that Contain Words - Loop through files in a folder - Write to new files - by dj99 - May-13-2019, 11:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 419 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
Question Right way to open files with different encodings? Winfried 2 237 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 327 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Using zipfile module - finding folders not files darter1010 2 282 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,072 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 821 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Class test : good way to split methods into several files paul18fr 4 489 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 567 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 471 Dec-22-2023, 07:17 AM
Last Post: dchilambo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020