Backing up/copying an entire folder tree in batch or python?

不羁的心 提交于 2019-12-11 11:21:11

问题


I'm trying to copy an entire directory from one locations to another via python every 7 days to essentially make a backup...

The backup folder/tree folder may or may not exist so it needs to create the folder if it doesn't exist, that's why I assumed distutils is better suited over shutil

Note Is it better for me to use batch or some other language for the said job?

The following code:

import distutils
distutils.dir_util.copy_tree("C:\Users\A\Desktop\Test", "C:\Users\A\Desktop\test_new", preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=1, verbose=0, dry_run=0)

Returns:

Traceback (most recent call last):
  File "C:\Users\A\Desktop\test.py", line 2, in <module>
    distutils.dir_util.copy_tree("C:\Users\A\Desktop\test", "C:\Users\A\Desktop\test2", preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=1, verbose=0, dry_run=0)
AttributeError: 'module' object has no attribute 'dir_util'

What am I doing wrong?

Thanks in advance - Hyflex


回答1:


For Unix/Linux, I suggest 'rsync'. For windows: xcopy




回答2:


You need to import dir_util specifically to access it's functions:

from distutils import dir_util

If there are other modules in that package that you need, add them to the line, separated by commas. Only import the modules you need.




回答3:


I've been attempting essentially the same thing to back up what I write on a plethora of virtual machines.

I ran into the same problem you did with distutils. From what I can tell, the Python community is using the distutils module to start standardizing how new modules interface with Python. I think they're still in the thick of it though as everything I've seen relating to it seems more complicated, not less complicated. Hopefully, I'm just seeing all the crazy that happens in the middle of a big change.

But I did figure out how to get it working. To use distutil.dir_util.copytree(),

>>> from distutils import dir_util
>>> dir_util.copy_tree("/home/user/backing_up/temp", "/home/user/backing_up/other")
['/home/user/backing_up/other/stuff.txt']     # Return value indicating success

If you feel like it's worthwhile, you can import distutils.core and make the longer call to distutils.dir_util.copy_tree().

>>> import distutils.core
>>> distutils.dir_util.copy_tree("/home/user/backing_up/temp", "/home/user/backing_up/other")
['/home/user/backing_up/other/stuff.txt']     # Return value indicating success

(I know, I know, there are subtle differences between "import module.submodule" and "from module import submodule" but that's not the intent of the question and so long as you're importing the correct stuff and calling the functions appropriately, it doesn't make a difference.)

Like you, I also explicitly stated that I wanted the default for preserve_mode and preserve_times, but I didn't touch the other variables. Everything worked as expected once I imported and called the function the way it wanted me to.

Now that my back up script works, I realize I should have written it in Bash since I plan on having it run whenever the machine goes to a specific runlevel. I'm using a wrapper instead now, even if I should just re-write it.



来源:https://stackoverflow.com/questions/19097235/backing-up-copying-an-entire-folder-tree-in-batch-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!