[Python-ideas] os.path.cansymlink(path)

Steven D'Aprano steve at pearwood.info
Thu Jul 24 19:43:41 CEST 2014


On Thu, Jul 24, 2014 at 07:51:08PM +0300, anatoly techtonik wrote:
> This is a live code from current virtualenv.py:
> 
>     if hasattr(os, 'symlink'):
>         logger.info('Symlinking Python bootstrap modules')
> 
> This code is wrong, because OS support for
> symlinks doesn't guarantee that mounted filesystem
> can do this, resulting in OSError at runtime. So, the
> proper check would be to check if specific path
> supports symlinking.
> 
> The idea is:
> 
>     os.path.cansymlink(path)  - Return True if filesystem
>         of specified path can be symlinked.
> 
> Yes/No/Opinions?

No. Even if the file system supports symlinks, doesn't mean that you can 
create one. You may not have privileges to create the symlink, or some 
other runtime error may occur.

Like most other file system operations, you should guard them with a 
try...except, not "Look Before You Leap".


-- 
Steven


More information about the Python-ideas mailing list