Skip to main content
Commonmark migration
Source Link

You should be able to do this with a simple [python][1]python script, something like:

import os

# Assuming that the replacement words are in a plain text file with
# colon separating the original & new words and one pair per line.
SEP = ":"
REPLACE_SOURCE = "/full/path/to/replacement/list.txt"
START_DIR = "."
with open(REPLACE_SOURCE) as infile:
    replacement_pairs = [line.strip().split(SEP) for line in infile]

for (root, dirs, files) in os.walk(START_DIR):
    print("Working in", root)
    for fname in files:
        fullpath = os.path.abspath(os.path.join(root, fname)
        if fname.lower.endswith('.txt') and fullpaht != REPLACE_SOURCE:
            print("\tProcessing", fname)
            intext = open(fullpath).read()
            for orig, repl in replacement_pairs:
                intext = intext.replace(orig, repl)
            # You may wish to consider renaming the original file here!
            with open(fullpath, 'wt') as outfile:
                outile.write(intext)

Warning the above will replace the contents of all .txt files under the current directory.

This should cope nicely unless some of the files are too big for the content to fit into the available memory.

Python is free, cross platform and open source. [1]: https://www.python.org/

You should be able to do this with a simple [python][1] script, something like:

import os

# Assuming that the replacement words are in a plain text file with
# colon separating the original & new words and one pair per line.
SEP = ":"
REPLACE_SOURCE = "/full/path/to/replacement/list.txt"
START_DIR = "."
with open(REPLACE_SOURCE) as infile:
    replacement_pairs = [line.strip().split(SEP) for line in infile]

for (root, dirs, files) in os.walk(START_DIR):
    print("Working in", root)
    for fname in files:
        fullpath = os.path.abspath(os.path.join(root, fname)
        if fname.lower.endswith('.txt') and fullpaht != REPLACE_SOURCE:
            print("\tProcessing", fname)
            intext = open(fullpath).read()
            for orig, repl in replacement_pairs:
                intext = intext.replace(orig, repl)
            # You may wish to consider renaming the original file here!
            with open(fullpath, 'wt') as outfile:
                outile.write(intext)

Warning the above will replace the contents of all .txt files under the current directory.

This should cope nicely unless some of the files are too big for the content to fit into the available memory.

Python is free, cross platform and open source. [1]: https://www.python.org/

You should be able to do this with a simple python script, something like:

import os

# Assuming that the replacement words are in a plain text file with
# colon separating the original & new words and one pair per line.
SEP = ":"
REPLACE_SOURCE = "/full/path/to/replacement/list.txt"
START_DIR = "."
with open(REPLACE_SOURCE) as infile:
    replacement_pairs = [line.strip().split(SEP) for line in infile]

for (root, dirs, files) in os.walk(START_DIR):
    print("Working in", root)
    for fname in files:
        fullpath = os.path.abspath(os.path.join(root, fname)
        if fname.lower.endswith('.txt') and fullpaht != REPLACE_SOURCE:
            print("\tProcessing", fname)
            intext = open(fullpath).read()
            for orig, repl in replacement_pairs:
                intext = intext.replace(orig, repl)
            # You may wish to consider renaming the original file here!
            with open(fullpath, 'wt') as outfile:
                outile.write(intext)

Warning the above will replace the contents of all .txt files under the current directory.

This should cope nicely unless some of the files are too big for the content to fit into the available memory.

Python is free, cross platform and open source.

Source Link
Steve Barnes
  • 31.9k
  • 2
  • 29
  • 64

You should be able to do this with a simple [python][1] script, something like:

import os

# Assuming that the replacement words are in a plain text file with
# colon separating the original & new words and one pair per line.
SEP = ":"
REPLACE_SOURCE = "/full/path/to/replacement/list.txt"
START_DIR = "."
with open(REPLACE_SOURCE) as infile:
    replacement_pairs = [line.strip().split(SEP) for line in infile]

for (root, dirs, files) in os.walk(START_DIR):
    print("Working in", root)
    for fname in files:
        fullpath = os.path.abspath(os.path.join(root, fname)
        if fname.lower.endswith('.txt') and fullpaht != REPLACE_SOURCE:
            print("\tProcessing", fname)
            intext = open(fullpath).read()
            for orig, repl in replacement_pairs:
                intext = intext.replace(orig, repl)
            # You may wish to consider renaming the original file here!
            with open(fullpath, 'wt') as outfile:
                outile.write(intext)

Warning the above will replace the contents of all .txt files under the current directory.

This should cope nicely unless some of the files are too big for the content to fit into the available memory.

Python is free, cross platform and open source. [1]: https://www.python.org/