diff options
author | André Erdmann <dywi@mailerd.de> | 2013-07-23 11:31:34 +0200 |
---|---|---|
committer | André Erdmann <dywi@mailerd.de> | 2013-07-23 11:31:34 +0200 |
commit | eb054c34b1af6b908d4df808742113d42e218d12 (patch) | |
tree | b834731a1ff83ca4925ff12483b22ded96bc1eea | |
parent | roverlay/remote/rsync: minor fixup (diff) | |
download | R_overlay-eb054c34b1af6b908d4df808742113d42e218d12.tar.gz R_overlay-eb054c34b1af6b908d4df808742113d42e218d12.tar.bz2 R_overlay-eb054c34b1af6b908d4df808742113d42e218d12.zip |
roverlay/util/common: try_unlink()
Remove a file if it exists and ignore errors caused by non-existence.
-rw-r--r-- | roverlay/util/common.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/roverlay/util/common.py b/roverlay/util/common.py index a2c155a..f27593a 100644 --- a/roverlay/util/common.py +++ b/roverlay/util/common.py @@ -8,9 +8,10 @@ __all__= [ 'dodir', 'for_all_files', 'get_dict_hash', 'keepenv', 'priosort', 'sysnop', - 'getsize', 'is_vcs_dir', 'headtail' + 'getsize', 'is_vcs_dir', 'headtail', 'try_unlink', ] +import errno import os import sys import logging @@ -26,6 +27,21 @@ def headtail ( iterable ): return ( iterable[0], iterable[1:] ) # --- end of headtail #py2 (...) --- +def try_unlink ( fspath ): + """Tries to remove a file. Does not fail if the file did not exist. + + arguments: + * fspath -- + """ + try: + os.unlink ( fspath ) + except OSError as oserr: + if oserr.errno == errno.ENOENT: + pass + else: + raise +# --- end of try_unlink (...) --- + def for_all_files ( files_or_dirs, func, args=(), kwargs={}, file_filter=None, ignore_missing=False |