Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

@Waylan I've tried using shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=shutil.copyfile, ignore_dangling_symlinks=False) but it yields the same result – SockPastaRock Feb 23, 2018 at 4:45

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
                Thanks for the insight. I've taken to using os.utime() to edit the st_mtime value of the directory in question to the current time using time.time() after I've copied it to its new location.
– SockPastaRock
                Feb 23, 2018 at 5:49
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.