Is it possible with pathlib to set file modification time?

169 Views Asked by At

I need to rewrite a lot of files, but I need them to keep their modification time as I need them indexed by time. I am aware that I could use os, but as I like pathlib's object oriented way, I would prefer to use pathlib if that is an option.

Pathlib-docs mention getting the st_mtime, but not setting it. I only found this description at tutorialspoint.com (last example) which uses an assignment method that seems not to work:

filepath.Path.stat().st_ctime = timestamp

Is it possible with pathlib or must I use os?

1

There are 1 best solutions below

0
On BEST ANSWER

No, it's not. pathlib is for manipulating paths, not files. It's useful to think of it as a convenience wrapper around path string.

Path.stat() is just a convenience method, doing nothing but calling os.stat() on path. So yes, you have to use os.utime().