Welcome, guest | Sign In | My Account | Store | Cart

Python function a la the Unix touch program (man touch).

Python, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def touch(path):
    import os, time
    now = time.time()
    try:
        # assume it's there
        os.utime(path, (now, now))
    except os.error:
        # if it isn't, try creating the directory,
        # a file with that name
        os.makedirs(os.path.dirname(path))
        open(path, "w").close()
        os.utime(path, (now, now))