Skip to content Skip to sidebar Skip to footer

Replace All Instances Of A Character With Another One In All Files Hierarchically In Directory Tree

so the command line will look like : ./replace.py current directory may be /home/user before = '\' after = '/' which means replacing backslash with / forward slash in ALL files.

Solution 1:

The problem is that str.endswith expects a string; you're passing a list. Try

returnany(fname.lower().endswith(extension) forextensionin replace_extensions)

Actually, it would be better to use os.path.splitext:

returnos.path.splitext(fname.lower())[1] in replace_extensions

Otherwise your code looks fine, although you'd do well to use a with expression for the output file as well:

withopen(out_fname, "w") asout:
        for line in f:
            out.write(re.sub(pat, s_after, line))

Post a Comment for "Replace All Instances Of A Character With Another One In All Files Hierarchically In Directory Tree"