You will probably find it easier to do this in bash (as in, once you understand the syntax it's only one or two lines), but for the record, here is how to do it in Python. You want to use two functions os.walk, and fnmatch.fnmatch to match the files you want in each directory. It looks like this:
import os, sys
from fnmatch import fnmatch
if len(sys.argv) != 2:
print "Usage:", sys.argv, "<directory>"
markdown = # <path to markdown.py>
directory = sys.argv
for path, directory, files in os.walk(directory):
for file in files:
if fnmatch(file, "*.html"):
html_file = "%s/%s" % (path, file)
markdown_file = html_file.replace(".html", ".markdown")
os.system("python %s %s > %s" % (markdown, markdown_file, html_file))
The main things to take away:
os.walk function traverses a directory structure (using an generator). It returns three variables:
- The current directory (
- The list of directories found in the current directory (
directories). You don't need this in this case.
- The list of files found in the current directory (
files). You do need this.
fnmatch.fnmatch function takes a list of files and tells you if it matches a pattern. This is a shell "glob" pattern, and not a regular expression. You can use regular expressions here, but
fnmatch is just easier for a simple case like this.
Note that you need to specify the path to the markdown script. Even better would be to not use
os.system but instead to import markdown the module and call it's primary function, but this generalizes to non-Python programs. (Plus, I don't know exactly what that function would be :).