我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.path.samefile()。
def maybe_relative_path(path): if not os.path.isabs(path): return path # already relative dir = path names = [] while True: prevdir = dir dir, name = os.path.split(prevdir) if dir == prevdir or not dir: return path # failed to make it relative names.append(name) try: if samefile(dir, os.curdir): names.reverse() return os.path.join(*names) except OSError: pass # ____________________________________________________________
def _samefile(f1, f2, callback = None): """ Tell whether two paths pointing to the same file @params: `f1`: the first path `f2`: the second path `callback`: the callback @returns: True if yes, otherwise False If any of the path does not exist, return False """ if not path.exists (f1) or not path.exists(f2): ret = False else: ret = path.samefile (f1, f2) if callback: # pragma: no cover callback(ret, f1, f2) return ret
def samefile (f1, f2, callback = None): """ Tell whether two paths pointing to the same file under locks @params: `f1`: the first path `f2`: the second path `callback`: the callback @returns: True if yes, otherwise False If any of the path does not exist, return False """ if f1 == f2: if callable(callback): callback(True, f1, f2) return True f1lock = _lockfile(f1) f2lock = _lockfile(f2) with filelock.FileLock(f1lock), filelock.FileLock(f2lock): ret = _samefile(f1, f2, callback) return ret
def _safeMove(src, dst, overwrite = True): """ Move a file/dir @params: `src`: The source file `dst`: The destination `overwrite`: Whether overwrite the destination @return: True if succeed else False """ if not path.exists(src): return False if path.exists(dst) and not path.samefile(src, dst) and overwrite: if path.isdir(dst) and not path.islink(dst): rmtree(dst) else: remove(dst) elif not path.exists(dst) and path.islink(dst): remove(dst) if not path.exists(dst): move (src, dst) return True
def _safeCopy(src, dst, overwrite = True): """ Copy a file/dir @params: `src`: The source file `dst`: The destination `overwrite`: Whether overwrite the destination @return: True if succeed else False """ if not path.exists(src): return False if path.exists(dst) and not path.samefile(src, dst) and overwrite: if path.isdir(dst) and not path.islink(dst): rmtree(dst) else: remove(dst) if not path.exists(dst): if path.isdir(src): copytree(src, dst) else: copyfile(src, dst) return True
def samefile(f1, f2): return os.path.abspath(f1) == os.path.abspath(f2)
def samefile(file1, file2): """ Emulated version of os.path.samefile. This is needed for Python 2.7 running on Windows (at least on Appveyor CI) """ return os.stat(file1) == os.stat(file2)
def test_render_tmp_dir(qtbot, setup_reports, report_file): """Test that rendered files are created in spyder's tempdir.""" reports = setup_reports output_file = reports._render_report(report_file) # Test that outfile is in spyder tmp dir assert osp.samefile(osp.commonprefix([output_file, TEMPDIR]), TEMPDIR)
def test_render_same_file(qtbot, setup_reports, report_file): """Test that re-rendered files are created in the same tempdir.""" reports = setup_reports output_file1 = reports._render_report(report_file) output_file2 = reports._render_report(report_file) assert osp.exists(output_file2) # Assert that file has been re-rendered in the same path assert osp.samefile(output_file1, output_file2)
def tests(self): return { # file testing 'is_dir': isdir, 'is_file': isfile, 'is_link': islink, 'exists': exists, 'link_exists': lexists, # path testing 'is_abs': isabs, 'is_same_file': samefile, 'is_mount': ismount, }