Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a file and preserve creation date in Python

I know that the creation date isn't stored in the filesystem itself, but I'm encountering the problem that when I use os.rename, it's updating the creation date of the files I'm working with.

Is it possible to rename a file without changing its original creation date?

like image 323
mlissner Avatar asked Dec 22 '22 13:12

mlissner


2 Answers

As said by Tudor you can use os.stat() and os.utime().

stat = os.stat(myfile)
# your code - rename access and modify your file
os.utime(my_new_file, (stat.st_atime, stat.st_mtime))
like image 169
karantan Avatar answered Dec 24 '22 03:12

karantan


You can read the timestamp before modifying it with os.stat(), keep it in som variable, rename the file, then change newfile's timestamp to the held value with os.utime()

like image 44
Tudor Constantin Avatar answered Dec 24 '22 02:12

Tudor Constantin