[英]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.我知道创建日期没有存储在文件系统本身中,但是我遇到的问题是,当我使用
os.rename
时,它正在更新我正在使用的文件的创建日期。
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() .正如 Tudor 所说,您可以使用os.stat()和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()
您可以在使用
os.stat()
修改时间戳之前读取时间戳,将其保存在 som 变量中,重命名文件,然后使用os.utime()
将新文件的时间戳更改为保存的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.