Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

touch a directory in python (Linux) [duplicate]

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.

like image 880
gnr Avatar asked May 24 '16 15:05

gnr


People also ask

What does touch command do in Python?

The touch command's primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch command creates a file only if the file doesn't already exist.

How do I get a list of files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.


2 Answers

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)
like image 120
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 03:10

Ignacio Vazquez-Abrams


You can do that with os.utime:

now = time.time()
os.utime('/tmp/marker', (now, now))
like image 27
myaut Avatar answered Oct 18 '22 04:10

myaut