# Chapter 9: Python Code for Preparing directories for output with @mkdir()

See also

- Manual Table of Contents
- mkdir() syntax
- formatter() syntax
- regex() syntax
- Back to Chapter 9: Preparing directories for output with @mkdir()

## Code for formatter() Zoo example

>     from ruffus import *

>     #   Make directories
>     @mkdir(["tiger", "lion", "dog", "crocodile", "rose"])
>     @originate(
>                 #   List of animals and plants
>                 [    "tiger/mammals.wild.animals",
>                     "lion/mammals.wild.animals",
>                     "lion/mammals.handreared.animals",
>                     "dog/mammals.tame.animals",
>                     "dog/mammals.wild.animals",
>                     "crocodile/reptiles.wild.animals",
>                     "rose/flowering.handreared.plants"])
>     def create_initial_files(output_file):
>         with open(output_file, "w") as oo: pass

>     # create directories for each clade
>     @mkdir(    create_initial_files,                                       # Input

>                formatter(".+/(?P<clade>\w+).(?P<tame>\w+).animals"),       # Only animals: ignore plants!

>                "{subpath[0][1]}/{clade[0]}")                               # new_directory
>     #   Put different animals in different directories depending on their clade
>     @transform(create_initial_files,                                       # Input

>                formatter(".+/(?P<clade>\w+).(?P<tame>\w+).animals"),       # Only animals: ignore plants!

>                "{subpath[0][1]}/{clade[0]}/{tame[0]}.{subdir[0][0]}.food", # Replacement

>                "{subpath[0][1]}/{clade[0]}",                               # new_directory
>                "{subdir[0][0]}",                                           # animal_name
>                "{tame[0]}")                                                # tameness
>     def feed(input_file, output_file, new_directory, animal_name, tameness):
>             print "%40s -> %90s" % (input_file, output_file)
>             # this works now
>             open(output_file, "w")

>     pipeline_run(verbose=0)

## Code for regex() Zoo example

>     from ruffus import *

>     #   Make directories
>     @mkdir(["tiger", "lion", "dog", "crocodile", "rose"])
>     @originate(
>                 #   List of animals and plants
>                 [    "tiger/mammals.wild.animals",
>                     "lion/mammals.wild.animals",
>                     "lion/mammals.handreared.animals",
>                     "dog/mammals.tame.animals",
>                     "dog/mammals.wild.animals",
>                     "crocodile/reptiles.wild.animals",
>                     "rose/flowering.handreared.plants"])
>     def create_initial_files(output_file):
>         with open(output_file, "w") as oo: pass

>     # create directories for each clade
>     @mkdir(    create_initial_files,                                        # Input
>                regex(r"(.*?/?)(\w+)/(?P<clade>\w+).(?P<tame>\w+).animals"), # Only animals: ignore plants!
>                r"\g<clade>")                                                # new_directory
>     #   Put different animals in different directories depending on their clade
>     @transform(create_initial_files,                                        # Input
>                regex(r"(.*?/?)(\w+)/(?P<clade>\w+).(?P<tame>\w+).animals"), # Only animals: ignore plants!
>                r"\1\g<clade>/\g<tame>.\2.food",                             # Replacement
>                r"\1\g<clade>",                                              # new_directory
>                r"\2",                                                       # animal_name
>                "\g<tame>")                                                  # tameness
>     def feed(input_file, output_file, new_directory, animal_name, tameness):
>         print "%40s -> %90s" % (input_file, output_file)
>         # this works now
>         open(output_file, "w")

>     pipeline_run(verbose=0)