3

I have a parent directory, and I'd like to go through that directory and grab each file with a specific string for editing in python. I have been using grep -r 'string' filepath in terminal, but I want to be able to do everything using python. I'm hoping to get all the files into an array and go through each of them to edit them.

Is there a way to do this by only running a python script?

4
  • You can also feed grep an array of files: like grep 'pattern' file1 file2 file3. Commented Jan 19, 2017 at 16:25
  • or grep "pattern" file*. If you choose python, I suggest that you perform the grep operation natively using python and string/regex features so you don't depend on grep which is not installed by default on windows for instance, you don't handle subprocess output... only advantages. Commented Jan 19, 2017 at 16:25
  • 1
    But you want to do this in a terminal or in a python script? It's not clear from the title. Commented Jan 19, 2017 at 16:26
  • I want to do this in a python script. The goal is to find then mass edit those files. Commented Jan 19, 2017 at 16:32

2 Answers 2

1

changing current folder to parent

import os    
os.chdir("..")

changing folder

import os
os.chdir(dir_of_your_choice)

finding files with a rule in the current folder

import glob
import os
current_dir = os.getcwd()
for f in glob.glob('*string*'):
    do_things(f)
Sign up to request clarification or add additional context in comments.

Comments

0
import os
#sourceFolder is the folder you're going to be looking inside for backslashes are a special character in python so they're escaped as double backslashes
sourceFolder = "C:\\FolderBeingSearched\\"

myFiles = []

# Find all files in the directory
for file in os.listdir(sourceFolder):
    myFiles.append(file)

#open them for editing
for file in myFiles:
    try:
        open(sourceFolder + file,'r')
    except:
        continue 

    #run whatever code you need to do on each open file here
    print("opened %s" % file)

EDIT: If you want to separate all files that contain a string (this just prints the list at the end currently):

import os
#sourceFolder is the folder you're going to be looking inside for backslashes are a special character in python so they're escaped as double backslashes
sourceFolder = "C:\\FolderBeingSearched\\"

myFiles = []
filesContainString = []
stringsImLookingFor = ['first','second']
# Find all files in the directory
for file in os.listdir(sourceFolder):
    myFiles.append(file)

#open them for editing
for file in myFiles:

    looking = sourceFolder + file

    try:
        open(looking,'r')        
    except:
        continue 

    print("opened %s" % file)

    found = 0
    with open(looking,encoding="latin1") as in_file:
        for line in in_file:
            for x in stringsImLookingFor:
                if line.find(x) != -1:
                    #do whatever you need to do to the file or add it to a list like I am
                    filesContainString.append(file)
                    found = 1
                    break
            if found:
                break

print(filesContainString)

1 Comment

Using this, how would you separate all the files from the files containing a specific string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.