Saving file attributes to a copy

I have a situation where I want to save the original attributes in a file (file creation date, etc.). Usually, when you copy files to Windows, the copy you create gets new “changed” dates, etc. I came through the shutil.copy command, although this does not keep the file attributes the same.

I found the following question on Stack Unix , but I was wondering if there is a way to do this in Python.

+8
python file-attributes
source share
1 answer

If you look at the documentation for shutil , you will immediately find the copy2 function, which:

copy() is copy2() , except that copy2() also tries to save all file metadata.

In recent versions of Python, there are many functions for executing bits and parts of this separately - copy , copymode , copystat , but if you just want to copy everything, copy2 makes everything possible.

As warns at the top of the documentation, “anything is possible” does not mean everything, but includes dates and other attributes. In particular:

On Windows, files, ACLs, and alternate data streams are not copied.

If you really need to include even this stuff, you will need to access the Win32 API (which is easiest to do with pywin32 ). But you do not.

+16
source share

All Articles