如何在Python中重命名文件并保留创建日期 [英] How to rename a file and preserve creation date in Python

查看:199
本文介绍了如何在Python中重命名文件并保留创建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道创建日期并不存储在文件系统本身中,但是遇到一个问题,当我使用os.rename时,它会更新正在使用的文件的创建日期.

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?

推荐答案

正如Tudor所说,您可以使用 os.stat() os. utime().

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

这篇关于如何在Python中重命名文件并保留创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆