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:
- Get all absolute paths to all files of source directory
- Get all sizes of all files
- Sort all files (regardless of location) by size
- Copy files from location X to tmp location (until sum of copied files is <= to max archive size)
- Create archive of tmp
- Clean tmp location
- Go back to 4 while there are still files to copy
Any feedback would be appreciated. Thanks.
Copying parts of a directory tree, you say? In that case, try
shutil.copytree().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(); theignoreargument 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
ignorecallable, that will also affect what is copied (as you can see in thecopytree()source code).(Copying/paraphrasing from comments)
As the implementation of
shutil.copytree()shows howcopytree()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 ifcopytree()itself is not useful.