Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Put all this code into a file called <code>mass_replace</code>. Under Linux or Mac OS X, you can do <code>chmod +x mass_replace</code> and then just run this. Under Windows, you can run it with <code>python mass_replace</code> followed by the appropriate arguments.</p> <pre><code>#!/usr/bin/python import os import re import sys # list of extensions to replace DEFAULT_REPLACE_EXTENSIONS = None # example: uncomment next line to only replace *.c, *.h, and/or *.txt # DEFAULT_REPLACE_EXTENSIONS = (".c", ".h", ".txt") def try_to_replace(fname, replace_extensions=DEFAULT_REPLACE_EXTENSIONS): if replace_extensions: return fname.lower().endswith(replace_extensions) return True def file_replace(fname, pat, s_after): # first, see if the pattern is even in the file. with open(fname) as f: if not any(re.search(pat, line) for line in f): return # pattern does not occur in file so we are done. # pattern is in the file, so perform replace operation. with open(fname) as f: out_fname = fname + ".tmp" out = open(out_fname, "w") for line in f: out.write(re.sub(pat, s_after, line)) out.close() os.rename(out_fname, fname) def mass_replace(dir_name, s_before, s_after, replace_extensions=DEFAULT_REPLACE_EXTENSIONS): pat = re.compile(s_before) for dirpath, dirnames, filenames in os.walk(dir_name): for fname in filenames: if try_to_replace(fname, replace_extensions): fullname = os.path.join(dirpath, fname) file_replace(fullname, pat, s_after) if len(sys.argv) != 4: u = "Usage: mass_replace &lt;dir_name&gt; &lt;string_before&gt; &lt;string_after&gt;\n" sys.stderr.write(u) sys.exit(1) mass_replace(sys.argv[1], sys.argv[2], sys.argv[3]) </code></pre> <p>EDIT: I have changed the above code from the original answer. There are several changes. First, <code>mass_replace()</code> now calls <code>re.compile()</code> to pre-compile the search pattern; second, to check what extension the file has, we now pass in a tuple of file extensions to <code>.endswith()</code> rather than calling <code>.endswith()</code> three times; third, it now uses the <code>with</code> statement available in recent versions of Python; and finally, <code>file_replace()</code> now checks to see if the pattern is found within the file, and doesn't rewrite the file if the pattern is not found. (The old version would rewrite every file, changing the timestamps even if the output file was identical to the input file; this was inelegant.)</p> <p>EDIT: I changed this to default to replacing every file, but with one line you can edit to limit it to particular extensions. I think replacing every file is a more useful out-of-the-box default. This could be extended with a list of extensions or filenames not to touch, options to make it case insensitive, etc.</p> <p>EDIT: In a comment, @asciimo pointed out a bug. I edited this to fix the bug. <code>str.endswith()</code> is documented to accept a tuple of strings to try, but not a list. Fixed. Also, I made a couple of the functions accept an optional argument to let you pass in a tuple of extensions; it should be pretty easy to modify this to accept a command-line argument to specify which extensions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload