我们从Python开源项目中,提取了以下34个代码示例,用于说明如何使用os.path.lexists()。
def execute(self): from os.path import join, expanduser, lexists from os import makedirs dirname = join(self.fm.thisdir.path, expanduser(self.rest(1))) if not lexists(dirname): makedirs(dirname) else: self.fm.notify("file/directory exists!", bad=True)
def execute(self): from os.path import join, expanduser, lexists fname = join(self.fm.thisdir.path, expanduser(self.rest(1))) if not lexists(fname): open(fname, 'a').close() else: self.fm.notify("file/directory exists!", bad=True)
def mkdir(path): """Create a directory if it does not pre-exist, otherwise, pass.""" if not op.exists(path) and not op.lexists(path): try: os.makedirs(path) except OSError as e: # "File exists error" can happen when python # fails to syncronize with NFS or multiple # processes are trying to make the same dir. if e.errno == 17: pass else: raise OSError(e)
def ln(src, dst): """if src and dst are identical, pass. Otherwise, create dst, a soft symbolic link pointing to src.""" if realpath(src) != realpath(dst): if op.exists(dst) or op.lexists(dst): os.remove(dst) logging.debug("Creating a symbolic link {dst} pointing to {src}". format(dst=dst, src=src)) os.symlink(realpath(src), realpath(dst))
def execute(self): from os.path import join, expanduser, lexists from os import mkdir dirname = join(self.fm.thisdir.path, expanduser(self.rest(1))) if not lexists(dirname): mkdir(dirname) else: self.fm.notify("file/directory exists!", bad=True)
def GetDirectory(multiple=False, selected=None, sep=None, **kwargs): """Prompt the user for a directory. This will raise a Zenity Directory Selection Dialog. It will return a list with the selected directories or None if the user hit cancel. multiple - True to allow the user to select multiple directories. selected - Path to the directory to be selected on startup. sep - Token to use as the path separator when parsing Zenity's return string. kwargs - Optional command line parameters for Zenity such as height, width, etc.""" args = ['--directory'] if multiple: args.append('--multiple') if selected: if not path.lexists(selected): raise ValueError("File %s does not exist!" % selected) args.append('--filename=%s' % selected) if sep: args.append('--separator=%s' % sep) for generic_args in kwargs_helper(kwargs): args.append('--%s=%s' % generic_args) p = run_zenity('--file-selection', *args) if p.wait() == 0: return p.stdout.read().strip().split('|')
def symlink_unless_exists(link_tgt, link_loc): """ Create a symlink at link_loc pointing to link_tgt unless file already exists. :param link_tgt: link target :param link_loc: link location """ if not osp.lexists(link_loc): os.symlink(link_tgt, link_loc)
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, }
def ensureabsent(path): """ If path exists remove it whether it's a file or a folder """ if exists(path): if israwdir(path): shutil.rmtree(path) else: os.remove(path)
def removeuneeded(old,target,rel,entry,skip): """ Remove an entry that isn't needed by new if it's from old If there where any issues this returns True otherwise it returns False. """ oldentry=joinpath(old,entry) targetentry=joinpath(target,entry) relentry=joinpath(rel,entry) matchentry=relentry if israwdir(targetentry): matchentry+="/" # only remove if was from old entries. Leave anything else behind (assume it was created by the game) # things in skip are treated as if not in old or new even if they actually are if exists(oldentry) and not matchespatterns(matchentry,skip): # old and target but no new so remove or if changed append .~local, if israwdir(targetentry): if isdir(old): # target and old dirs but no new so need to recurse if checkandcleartrees(relentry,targetentry,oldentry,skip): # target and old both dirs and target now empty os.rmdir(targetentry) else: print("WARNING: directory {0} from old isn't wanted in new but some files weren't removed".format(targetentry)) return True else: # target is dir but old isn't ensureabsent(targetentry+LOCALSUFFIX) os.rename(targetentry,targetentry+LOCALSUFFIX) print("WARNING: file from old version is folder in local but isn't wanted in new.\nRenamed to '{0}".format(targetentry+LOCALSUFFIX)) return True elif islink(targetentry) or checkfiles(targetentry,oldentry): # link or unchanged file os.remove(targetentry) else: # not dir and not link and changed so rename ensureabsent(targetentry+LOCALSUFFIX) os.rename(targetentry,targetentry+LOCALSUFFIX) print("WARNING: File from old version has been changed in local but isn't wanted in new.\nRenamed to '{0}".format(targetentry+LOCALSUFFIX)) return True return False
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, }
def _resolve_file(self, path): """Given a path, return path of the repository it belongs to""" # very naive just to get a ball rolling if not isabs(path): raise ValueError("ATM operating on full paths, got %s" % path) dirpath = path if isdir(path) else dirname(path) # quick check first if dirpath in self._known_repos: return self._known_repos[dirpath] # it could still be a subdirectory known to the repository known above it for repo in self._known_repos.values(): # XXX this design is nohow accounts for some fancy cases where # someone could use GIT_TREE and other trickery to have out of the # directory checkout. May be some time we would get there but # for now # should be ok # could be less efficient than some str manipulations but ok for now if os.path.commonprefix((repo.path, path)) != repo.path: continue # could be less efficient than some str manipulations but ok for now # since we rely on a strict check (must be registered within the repo) if repo.owns_path(path): return repo # ok -- if it is not among known repos, we need to 'sniff' around # if there is a repository at that path for Shim in self.SHIMS: lgr.log(5, "Trying %s for path %s", Shim, path) shim = Shim.get_at_dirpath(self._session, dirpath) \ if lexists(dirpath) else None if shim: # so there is one nearby -- record it self._known_repos[shim.path] = shim # but it might still not to know about the file if shim.owns_path(path): return shim # if not -- just keep going to the next candidate repository return None