Implement touch using Python? Implement touch using Python? python python

Implement touch using Python?


Looks like this is new as of Python 3.4 - pathlib.

from pathlib import PathPath('path/to/file.txt').touch()

This will create a file.txt at the path.

--

Path.touch(mode=0o777, exist_ok=True)

Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.


This tries to be a little more race-free than the other solutions. (The with keyword is new in Python 2.5.)

import osdef touch(fname, times=None):    with open(fname, 'a'):        os.utime(fname, times)

Roughly equivalent to this.

import osdef touch(fname, times=None):    fhandle = open(fname, 'a')    try:        os.utime(fname, times)    finally:        fhandle.close()

Now, to really make it race-free, you need to use futimes and change the timestamp of the open filehandle, instead of opening the file and then changing the timestamp on the filename (which may have been renamed). Unfortunately, Python doesn't seem to provide a way to call futimes without going through ctypes or similar...


EDIT

As noted by Nate Parsons, Python 3.3 will add specifying a file descriptor (when os.supports_fd) to functions such as os.utime, which will use the futimes syscall instead of the utimes syscall under the hood. In other words:

import osdef touch(fname, mode=0o666, dir_fd=None, **kwargs):    flags = os.O_CREAT | os.O_APPEND    with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:        os.utime(f.fileno() if os.utime in os.supports_fd else fname,            dir_fd=None if os.supports_fd else dir_fd, **kwargs)


def touch(fname):    if os.path.exists(fname):        os.utime(fname, None)    else:        open(fname, 'a').close()