Search code examples
pythonregexbatch-processing

How to batch substitute lines in many files in python?


I have several folders (with several sub-folders) of .md files, which share the same basic structure:

###### v1
Blah blah 
###### v2
Blah blah 
...

And I want to add the base folder's name e.g., NET, ESV, to the end of each header:

###### v1 NET 
Blah blah 
###### v2 NET 
Blah blah 
...

This is my amateur attempt at Python, which currently fails:

import os, re, fileinput 

for root, dirs, files in os.walk("/NET"): 
    for file in files: 
        if file.endswith(".md"): 
            for line in fileinput(files=file, inplace=1, backup='.bak'):
                line = re.sub(r'(#+ v\d{1,3})( )', r'\1\2NET')

Suggestions or tips for efficient python regex sub?


Solution

  • import os, re
    
    for root, dirs, files in os.walk("/NET"): 
        for file in files: 
            if not file.endswith(".md"):
                continue
            finame = os.path.join( root, file )
            foname = finame[:-3]+'.tmp'
            fout = open(foname,'w')
            for line in open(finame):
                line = re.sub(r'(#+ v\d{1,3}) ', r'\1  NET', line)
                fout.write( line )
            fout.close()
            os.remove( finame )
            os.rename( foname, finame )