Quick question: recursive touch?

Fredrik Lundh fredrik at pythonware.com
Tue Oct 5 15:42:33 EDT 1999


Preston Landers <prestonlanders at my-deja.com> wrote:
> Anyone know of a function equilivent in concept to a 'recursive
> touch?'
> 
> Where if I say:
> 
> touch foo/bar/baz.txt
> 
> If the directories foo and bar don't exist, create them, then create
> baz.txt inside of bar.

how about:

import os, time

def touch(file):
    now = time.time()
    try:
        # assume it's there
        os.utime(file, (now, now))
    except os.error:
        # if it isn't, try creating the directory,
        # a file with that name
        os.makedirs(os.path.dirname(file))
        open(file, "w").close()
        os.utime(file, (now, now))

touch("foo/bar/baz.txt")

</F>





More information about the Python-list mailing list