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?
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))
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()
精彩评论