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
#2
#!/usr/bin/python3
import os

def remove_line_from_file(filename, dirpath):
    filename  = os.path.join(dirpath, filename)
    temp_path = os.path.join(dirpath, filename + '.tmp')
    search_keywords = ['Car','Train']

    with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:
        for line in f_read:
            found = False
            for word in search_keywords:
                if word in line:
                    found = True
            if not found:
                temp.write(line)

    #os.rename(temp_path, filename)

#main
directory = './'
for root, dirs, files in os.walk(directory):
    for f in files:
        remove_line_from_file(f, directory)
#done
Reply
#3
Thank you my dear friend heiner55.

I am so happy because I have been trying for days to work out the correct syntax for this.
I guess i didnt need the lambda thing
if (any(map(lambda word: word in line, search_keywords))):

Simplicity is the best

My files have so much garbage and it was very hard for me to find these line and manually delete them one after the other Wall

Thank you so much again for lending your programming eyes and time to help me.

Python hero!

I really apreciate it so much

Hope you have a great week! Big Grin

:)



:)


Python newbie trying to learn the ropes
Reply
#4
Glad to hear.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 474 Mar-11-2025, 06:58 PM
Last Post: snakes
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,393 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  how to download large files faster? kucingkembar 3 847 Feb-20-2025, 06:57 PM
Last Post: snippsat
  Inserting Python Buttons into KV Files edand19941 3 543 Feb-19-2025, 07:44 PM
Last Post: buran
Question [SOLVED] Right way to open files with different encodings? Winfried 3 4,303 Jan-18-2025, 02:19 PM
Last Post: Winfried
  Applications config files / Best practices aecordoba 2 2,098 Oct-23-2024, 12:56 PM
Last Post: aecordoba
  Compare 2 files for duplicates and save the differences cubangt 2 968 Sep-12-2024, 03:55 PM
Last Post: cubangt
  Convert Xls files into Csv in on premises sharpoint Andrew_andy9642 3 1,040 Aug-30-2024, 06:41 PM
Last Post: deanhystad
  deleting files in program files directory RRADC 6 3,077 Aug-21-2024, 06:11 PM
Last Post: snippsat
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,810 Aug-07-2024, 08:18 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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