Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:38:04+00:00 2026-06-16T17:38:04+00:00

Possible Duplicate: Search for string in txt file Python How, in python, can I

  • 0

Possible Duplicate:
Search for string in txt file Python

How, in python, can I delete a file if it contains a string.
I want it to go though all files in a directory, open them, check if they contain a string, if yes, deletes the file and moves onto the next one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:38:05+00:00Added an answer on June 16, 2026 at 5:38 pm

    There are a few pieces to this problem. First, you need a listing of the files. Depending on your needs: glob.glob, os.listdir or os.walk might be appropriate. You also need to know how to open a file and search for the string. The easiest (naive) way to do this is to open the file, read all the contents and check if the string is present:

    def check_file_for_string(filename,string):
        with open(filename) as fin:
             return string in fin.read()
    

    This naive way isn’t ideal as it will read the entire contents of your file into memory which could be a problem if the files are really big.

    Finally, you can use os.remove or os.unlink to delete the file if check_file_for_string returns True.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Oracle Search all tables all columns for string I'm looking for a
Possible Duplicate: Get a list of all files inside of a directory in vb.net
Possible Duplicate: How to recursively list all the files in a directory in C#?
Possible Duplicate: How to search for a string in files with Ant (make sure
Possible Duplicate: How to search for string in MS Access VBA array I am
Possible Duplicate: Windows Phone 7 Search Button I think that all Windows Phone mobiles
Possible Duplicate: How can I disable/enable UISearchBar keyboard's Search button? Can we disable the
Possible Duplicate: Find text string in jQuery and make it bold How can I
Possible Duplicate: Regex.IsMatch vs string.Contains Which is faster, preferable and why? What the difference
Possible Duplicate: How can I search a generic TList for a record with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.