Reputation: 3357
I'm using Python 2.7.3. I'm working on a Python script to copy parts of a directory tree from one location to another. Some of the files that need to be copied are symlinks.
How can I use Python to copy symlinks from location to another without following them? (I just want it to blindly copy them just like they are 'regular' files)
I found that shutil.copy()
for Python 3.3 supports the argument follow_symlinks=False
, yet older versions of shutil doesn't.
EDIT: More details:
The purpose of this script is to take all files from a specified location and split them up into individual archives. I know I could do this by zipping up the entire directory and splitting the archive, but I need to have the capability of extracting each archive individually without rejoining into one large archive. Also, each archive must be less than a specified size.
Basic approach:
Any feedback would be appreciated. Thanks.
Upvotes: 4
Views: 4746
Reputation: 21089
Copying parts of a directory tree, you say? In that case, try shutil.copytree()
.
If symlinks is true, symbolic links in the source tree are represented as symbolic links in the new tree, but the metadata of the original links is NOT copied; if false or omitted, the contents and metadata of the linked files are copied to the new tree.
So as long as you don't need to keep the metadata the same, this should work just fine (actually, can symlinks on their own even have such metadata, or do they just reference the metadata of the file/object they point to?). Also note that you don't have to copy the entire tree with copytree()
; the ignore
argument allows you to provide a callable that will prune down the files and directories to copy.
One thing to watch out for, though: If you modify the contents list passed in to the ignore
callable, that will also affect what is copied (as you can see in the copytree()
source code).
(Copying/paraphrasing from comments)
As the implementation of shutil.copytree()
shows how copytree()
handles symbolic links (linkto = os.readlink(srcname); os.symlink(linkto, dstname)
), that can be used as a reference for how to "copy" symbolic links even if copytree()
itself is not useful.
Upvotes: 4