mlissner
mlissner

Reputation: 18166

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?

Upvotes: 7

Views: 5944

Answers (2)

karantan
karantan

Reputation: 1015

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

Upvotes: 13

Tudor Constantin
Tudor Constantin

Reputation: 26861

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

Upvotes: 2

Related Questions