6

Is there a way to "touch" a existing directory in Linux using python so that its modtime becomes the current system time?

From the command line this is the equivalent of touch $directory.

0

2 Answers 2

8

os.utime() will allow you to set the atime and mtime of an existing filesystem object, defaulting to the current date and time.

os.utime(path)
Sign up to request clarification or add additional context in comments.

2 Comments

Works for py3 but py2, have to specify the atime mtime tuple
Actually, for py2, while you do need a 2nd argument, it can be simply None to get the current date and time.
3

You can do that with os.utime:

now = time.time()
os.utime('/tmp/marker', (now, now))

2 Comments

No need to cast now time.time() to int
No need to use time.time() at all -- as of Python 2.0, you can get the same effect with simply os.utime('/tmp/marker', None).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.