开发者

How to rename a file and preserve creation date in Python

开发者 https://www.devze.com 2023-03-08 09:17 出处:网络
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 wor

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号