Enable unlinking of symbolic links to folders in Windows #199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It appears that in Python 2.x on Windows
os.unlink(path)
can't remove symbolic links to folders, and it's necessary to useos.rmdir(path)
instead. In Python 3.x, this appears to have been fixed along with generally adding support foros.symlink
in Windows.This patch adds special casing to
LocalPath.unlink
to enable deleting symbolic links to folders in Windows. I chose to duplicate thehasattr(os, "symlink")
feature-detect from theLocalPath.symlink
method, as that will still useos.unlink
all the time ifos.symlink
is available (Python 3.x, or Python 2.x on Unix-y systems). Additionally, ifnot self.isdir()
, such as for files or for symbolic links to files, we still callos.unlink
as before.A side-effect of this change is that for Python 2.x on Windows, calling
LocalPath.unlink
on an empty directory (the actual directory, not a symlink to it) will now callos.rmdir
instead ofos.unlink
, which will now successfully remove the directory. but wheneveros.symlink
is available, this would no longer work. Any currently-existing code would be callingLocalPath.delete
, though, to delete directories.Below are two REPL sessions demonstrating the behavior. The first is Python 2.7, and the second is Python 3.4. Both are in Windows 7. Beforehand, I created a file called
testfile
and a folder calledtestfolder
.In Python 2.7,
os.symlink
is not available, so I need to create the symlinks via plumbum. I testedos.rmdir
for the file symlink to see if perchancermdir
might be used for both cases (it can't).In Python 3.4,
os.symlink
is available andos.unlink
works for symlinks to both files and folders, so no trouble there:Another course I considered was that some other LocalPath method might work, e.g. I could just call
LocalPath.delete(path)
for symlinks. This unfortunately doesn't work, as I wind up atplumbum/plumbum/path/local.py
Line 120 in 5fe7995
shutil.rmtree
on Windows deletes not just the symlink but also the entire folder that the symlink points to (eek).PS: Thanks for making this awesome library!