我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用errno.EISDIR。
def from_pyfile(self, filename, silent=False): """Updates the values in the config from a Python file. :param filename: the filename of the config. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. """ filename = os.path.join(self.root_path, filename) d = types.ModuleType('config') d.__file__ = filename try: with open(filename) as config_file: exec(compile(config_file.read(), filename, 'exec'), d.__dict__) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise self.from_object(d) return True
def from_json(self, filename, silent=False): """Updates the values in the config from a JSON file. This function behaves as if the JSON object was a dictionary and passed to the :meth:`from_mapping` function. :param filename: the filename of the JSON file. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. .. versionadded:: 0.11 """ filename = os.path.join(self.root_path, filename) try: with open(filename) as json_file: obj = json.loads(json_file.read()) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise return self.from_mapping(obj)
def _ensure_tree(path): """Create a directory (and any ancestor directories required). :param path: Directory to create """ try: os.makedirs(path) except OSError as e: if e.errno == errno.EEXIST: if not os.path.isdir(path): raise else: return False elif e.errno == errno.EISDIR: return False else: raise else: return True
def unregister(self,name): origname,name=name,self.validateName(name) fn=self.translate(name) self.lock.acquire() try: try: os.remove(fn) self._dosynccall("unregister",origname) Log.msg('NameServer','unregistered',name) except OSError,x: if x.errno==errno.ENOENT: raise Pyro.errors.NamingError('name not found',name) elif x.errno==errno.EISDIR: Log.msg('NameServer','attempt to remove a group:',name) raise Pyro.errors.NamingError('is a group, not an object',name) else: raise Pyro.errors.NamingError(str(x)) finally: self.lock.release()
def run(self, sync): app = sync.app with app.db.getSession() as session: change = session.getChange(self.key) if not change: return repo = gitrepo.get_repo(change.project.name, app.config) self.log.info("Pruning %s change %s status:%s updated:%s" % ( change.project.name, change.number, change.status, change.updated)) change_ref = None for revision in change.revisions: if change_ref is None: change_ref = '/'.join(revision.fetch_ref.split('/')[:-1]) self.log.info("Deleting %s ref %s" % ( change.project.name, revision.fetch_ref)) repo.deleteRef(revision.fetch_ref) self.log.info("Deleting %s ref %s" % ( change.project.name, change_ref)) try: repo.deleteRef(change_ref) except OSError as e: if e.errno not in [errno.EISDIR, errno.EPERM]: raise session.delete(change)
def remove(path, recurse=True): """ Equivalent to rm -f or rm -rf NOTE: be careful about passing paths that may contain filenames with wildcards in them (as opposed to passing an actual wildcarded path) - since we use glob.glob() to expand the path. Filenames containing square brackets are particularly problematic since the they may not actually expand to match the original filename. """ for name in glob.glob(path): try: os.unlink(name) except OSError as exc: if recurse and exc.errno == errno.EISDIR: shutil.rmtree(name) elif exc.errno != errno.ENOENT: raise
def duplicate(src, dst): """Alternative to shutil.copyfile, this will use os.link when possible. See shutil.copyfile for documention. Only `src` and `dst` are supported. Unlike copyfile, this will not overwrite the destination if it exists. """ if os.path.isdir(src): # os.link will give a permission error raise OSError(errno.EISDIR, "Is a directory", src) if os.path.isdir(dst): # os.link will give a FileExists error raise OSError(errno.EISDIR, "Is a directory", dst) if os.path.exists(dst): # shutil.copyfile will overwrite the existing file raise OSError(errno.EEXIST, "File exists", src, "File exists", dst) try: os.link(src, dst) except OSError as e: if e.errno == errno.EXDEV: # Invalid cross-device link shutil.copyfile(src, dst) else: raise
def from_json(self, filename, silent=False): """Updates the values in the config from a JSON file. This function behaves as if the JSON object was a dictionary and passed to the :meth:`from_mapping` function. :param filename: the filename of the JSON file. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. .. versionadded:: 1.0 """ filename = os.path.join(self.root_path, filename) try: with open(filename) as json_file: obj = json.loads(json_file.read()) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise return self.from_mapping(obj)
def check_path(path, ptype=None, access=None): """Checks that a path exists, is of the specified type, and allows the specified access. Args: ptype: 'f' for file or 'd' for directory. access (int): One of the access values from :module:`os` Raises: IOError if the path does not exist, is not of the specified type, or doesn't allow the specified access. """ if ( ptype == 'f' and not path.startswith("/dev/") and not os.path.isfile(path)): raise IOError(errno.EISDIR, "{} is not a file".format(path), path) elif ptype == 'd' and not os.path.isdir(path): raise IOError(errno.ENOTDIR, "{} is not a directory".format(path), path) elif not os.path.exists(path): raise IOError(errno.ENOENT, "{} does not exist".format(path), path) if access is not None and not os.access(path, access): raise IOError(errno.EACCES, "{} is not accessable".format(path), path) return path
def copy_path(src, dst): ''' Copy file or directory `src` to `dst`. This is equivalent to `shutil.copyfile` if `src` is a file or `shutil.copytree` if `dst` is a directory. src: The source file or directory. dst: The destination. ''' try: shutil.copyfile(src, dst) except OSError as exc: if exc.errno == errno.EISDIR: shutil.copytree(src, dst) else: raise
def from_yaml(self, filename, silent=False): """ Updates the values in the config from a yaml file. :param filename: filename of the config. :param silent: set to ``True`` if you want silent failure for missing files. """ filename = os.path.join(filename) try: with open(filename) as conf_yaml: self.from_dict(yaml.safe_load(conf_yaml)) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise return True
def errnoToFailure(e, path): """ Map C{OSError} and C{IOError} to standard FTP errors. """ if e == errno.ENOENT: return defer.fail(FileNotFoundError(path)) elif e == errno.EACCES or e == errno.EPERM: return defer.fail(PermissionDeniedError(path)) elif e == errno.ENOTDIR: return defer.fail(IsNotADirectoryError(path)) elif e == errno.EEXIST: return defer.fail(FileExistsError(path)) elif e == errno.EISDIR: return defer.fail(IsADirectoryError(path)) else: return defer.fail()
def openForReading(self, path): """ Open C{path} for reading. @param path: The path, as a list of segments, to open. @type path: C{list} of C{unicode} @return: A L{Deferred} is returned that will fire with an object implementing L{IReadFile} if the file is successfully opened. If C{path} is a directory, or if an exception is raised while trying to open the file, the L{Deferred} will fire with an error. """ p = self._path(path) if p.isdir(): # Normally, we would only check for EISDIR in open, but win32 # returns EACCES in this case, so we check before return defer.fail(IsADirectoryError(path)) try: f = p.open('r') except (IOError, OSError) as e: return errnoToFailure(e.errno, path) except: return defer.fail() else: return defer.succeed(_FileReader(f))
def openForWriting(self, path): """ Open C{path} for writing. @param path: The path, as a list of segments, to open. @type path: C{list} of C{unicode} @return: A L{Deferred} is returned that will fire with an object implementing L{IWriteFile} if the file is successfully opened. If C{path} is a directory, or if an exception is raised while trying to open the file, the L{Deferred} will fire with an error. """ p = self._path(path) if p.isdir(): # Normally, we would only check for EISDIR in open, but win32 # returns EACCES in this case, so we check before return defer.fail(IsADirectoryError(path)) try: fObj = p.open('w') except (IOError, OSError) as e: return errnoToFailure(e.errno, path) except: return defer.fail() return defer.succeed(_FileWriter(fObj))
def from_pyfile(self, filename, silent=False): """Updates the values in the config from a Python file. This function behaves as if the file was imported as module with the :meth:`from_object` function. :param filename: the filename of the config. This can either be an absolute filename or a filename relative to the root path. :param silent: set to `True` if you want silent failure for missing files. .. versionadded:: 0.7 `silent` parameter. """ filename = os.path.join(self.root_path, filename) d = imp.new_module('config') d.__file__ = filename try: with open(filename) as config_file: exec(compile(config_file.read(), filename, 'exec'), d.__dict__) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = 'Unable to load configuration file (%s)' % e.strerror raise self.from_object(d) return True