I am trying to copy a directory to a new location. When it arrives in the new location, I want the last modified date for the new directory to be the time that it was copied to the new location.
Looking at the documentation (https://docs.python.org/3/library/shutil.html I am using python 3.6), shutil.copytree takes an argument "copy_function" which is "shutil.copy2" by default so as to maintain most metadata (like the modified date). However changing this to "shutil.copy" i.e:
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=shutil.copy, ignore_dangling_symlinks=False)
appears to still maintain the modified date of the original file. Even though shutil.copy() should not maintain metadata beyond file permissions according to the documentation.
Interestingly, watching the destination of the copy, I can see that the directory momentarily has its modified date set to the current time before being reverted to the modified date of the source directory.
Thanks in advance for any help.
If you are trying to prevent directories from having their metadata copied, this is unavoidable. The copy_function is not called for directories in the first place, so changing it has no effect. copytree always calls copystat() on each directory in the tree, as can be seen in its source code. Note that this does not happen for files, because the function only recurses on directories (so the only way src will point to a regular file is if you pass a file as the argument).
If you're really desperate to make this work, you could monkey patch copystat() to do nothing, but I would hardly consider that a robust solution. It would be much safer to walk the destination directory structure and use os.utime() to reset each directory's modification time manually.
If you are trying to prevent files from having their metadata copied, I am unable to reproduce your problem:
kevin@instance-1 ~ % mkdir foo
kevin@instance-1 ~ % mkdir foo/bar
kevin@instance-1 ~ % touch foo/bar/baz --date 19700101
kevin@instance-1 ~ % ls -l foo/bar/baz
-rw-r--r-- 1 kevin kevin 0 Jan 1 1970 foo/bar/baz
kevin@instance-1 ~ % python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copytree('./foo', './qux', symlinks=False, ignore=None, copy_function=shutil.copy, ignore_dangling_symlinks=False)
'./qux'
>>>
kevin@instance-1 ~ % ls -l qux/bar/baz
-rw-r--r-- 1 kevin kevin 0 Feb 23 05:04 qux/bar/baz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With