Process a number of files in a directory. Catches any exception from the
    processing and checks if we should fail directly or keep going.
760526
def safe_process_files(path, files, args, state):
    
    for fn in files:
        full_fn = os.path.join(path, fn)
        try:
            if not process_file(path, fn, args, state):
                return False
        except Exception, e:
            sys.stderr.write("error: %s\n%s\n" % (os.path.join(path, fn), traceback.format_exc()))
            state.log_failed(full_fn)
        if state.should_quit():
            return False
    return True
231543
private void checkEmpty(Directory dir, Path pathForException) throws FileSystemException {
    if (!dir.isEmpty()) {
      throw new DirectoryNotEmptyException(pathForException.toString());
    }
  }
470421
@Nonnegative
    public static int count(@Nonnull final Path dir) throws IOException {
        try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            return Iterables.size(stream);
        } catch (DirectoryIteratorException ex) {
            // I/O error encounted during the iteration, the cause is an IOException
            throw ex.getCause();
        }
    }
180101
protected void check(File file) throws IOException {
        if(!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        
        if(file.isDirectory()) {
            throw new IOException("Cannot open directory " + file.getAbsolutePath());
        }
    }
765223
function(callback) {
      fs.readdir(args['<directory>'], function(err, files) {
        // error here means it does not exist, which is good.
        if (err || args['--force']) {
          return callback(null, args['<directory>']);
        }
        if (files.length > 0) {
          return callback(new Error(format('destination '
            + 'directory %s is not empty.', path.resolve(args['<directory>']))));
        }
        return callback(null, args['<directory>']);
      });
    }
1111735
public void processFile(String fileName, boolean failOnFileNotFound) throws Exception {

    boolean fileFound = false;
    InputStream f = null;
    try {
      String furl = null;

      File file = new File(fileName); // files in filesystem
      if (!file.exists()) {
        URL url = classLoadHelper.getResource(fileName);
        if (url != null) {
          try {
            furl = URLDecoder.decode(url.getPath(), "UTF-8");
          } catch (UnsupportedEncodingException e) {
            furl = url.getPath();
          }
          file = new File(furl);
          try {
            f = url.openStream();
          } catch (IOException ignor) {
            // Swallow the exception
          }
        }
      } else {
        try {
          f = new java.io.FileInputStream(file);
        } catch (FileNotFoundException e) {
          // ignore
        }
      }

      if (f == null) {
        fileFound = false;
      } else {
        fileFound = true;
      }
    } finally {
      try {
        if (f != null) {
          f.close();
        }
      } catch (IOException ioe) {
        logger.warn("Error closing jobs file " + fileName, ioe);
      }
    }

    if (!fileFound) {
      if (failOnFileNotFound) {
        throw new SchedulerException("File named '" + fileName + "' does not exist.");
      } else {
        logger.warn(
            "File named '"
                + fileName
                + "' does not exist. This is OK if you don't want to use an XML job config file.");
      }
    } else {
      processFile(fileName);
    }
  }
108592
def check_dir(directory, newly_created_files):
  """"""
  header_parse_failures = []
  for root, dirs, files in os.walk(directory):
    for f in files:
      if f.endswith('.py') and os.path.basename(f) != '__init__.py':
        filename = os.path.join(root, f)
        try:
          check_header(filename, filename in newly_created_files)
        except HeaderCheckFailure as e:
          header_parse_failures.append(e.message)
  return header_parse_failures
572057
def process_read_exception(exc, path, ignore=None):
    '''
    
    '''
    if ignore is not None:
        if isinstance(ignore, six.integer_types):
            ignore = (ignore,)
    else:
        ignore = ()

    if exc.errno in ignore:
        return

    if exc.errno == errno.ENOENT:
        raise CommandExecutionError('{0} does not exist'.format(path))
    elif exc.errno == errno.EACCES:
        raise CommandExecutionError(
            'Permission denied reading from {0}'.format(path)
        )
    else:
        raise CommandExecutionError(
            'Error {0} encountered reading from {1}: {2}'.format(
                exc.errno, path, exc.strerror
            )
        )
497219
protected void processDeleteOnExit() {
    synchronized (deleteOnExit) {
      for (Iterator<Path> iter = deleteOnExit.iterator(); iter.hasNext();) {
        Path path = iter.next();
        try {
          delete(path, true);
        }
        catch (IOException e) {
          LOG.info("Ignoring failure to deleteOnExit for path " + path);
        }
        iter.remove();
      }
    }
  }
137877
private function executeOnFiles(): void
    {
        foreach ($this->config->getFiles() as $source => $target) {
            try {
                $name = pathinfo($target)['filename'];
                $this->fileExecutor->invoke($source, $target, $name);
            } catch (Exception $exception) {
                $this->exceptionCatcher->catch($exception, $source);
            }
        }
    }
13265
public int process() throws Exception {
        int changed = 0;
        for (File child : files) {
            changed += (processFile(child) != null ? 1 : 0);
        }
        return changed;
    }
647512
private void processInputDirectories() {
        String[] inputdirs = getOptionValues(SHORT_OPT_IN_PATH);
        final List<File> files = new ArrayList<File>();
        final XBELFileFilter xbelFileFilter = new XBELFileFilter();
        final BELFileFilter belFileFilter = new BELFileFilter();

        for (String inputdir : inputdirs) {
            final File workingPath = new File(inputdir);
            if (!workingPath.canRead()) {
                error(BAD_INPUT_PATH + inputdir);
                failUsage();
            }

            final File[] xbels = workingPath.listFiles(xbelFileFilter);
            if (xbels != null && xbels.length != 0) {
                files.addAll(asList(xbels));
            }

            final File[] bels = workingPath.listFiles(belFileFilter);
            if (bels != null && bels.length != 0) {
                files.addAll(asList(bels));
            }
        }

        // Fail if none of the input directories contain BEL/XBEL files
        if (files.size() == 0) {
            error(NO_DOCUMENT_FILES);
            failUsage();
        }

        // Create the directory artifact or fail
        artifactPath = createDirectoryArtifact(outputDirectory, DIR_ARTIFACT);
        processFiles(files.toArray(new File[0]));
    }
1356187
def check_dirs():
    
    dirs = [MAIN_DIR, TEMP_DIR, DOWNLOAD_DIR, SAVESTAT_DIR]
    for directory in dirs:
        if directory.exists() and not directory.is_dir():
            raise FileExistsError(str(directory.resolve()) + " cannot be used as a directory.")
391130
def process_file(self, path, dryrun):
        
        # special case - skip output file so we won't include it in result
        if path == self._output_path:
            return None

        # if dryrun skip and return file
        if dryrun:
            return path

        # concat file with output file
        with open(path, "rb") as infile:
            data = infile.read()
            self._output_file.write(data)

        # return processed file path
        return path
1221189
public static void directoryCheck(String dir) throws IOException {
		final File file = new File(dir);

		if (!file.exists()) {
			FileUtils.forceMkdir(file);
		}
	}
264349
def process_dir(self, dirname):
        
        ret = []
        sub_rec = [f for f in os.listdir(dirname)]
        for fname in sub_rec:
            full_path = os.path.join(dirname, fname)

            if os.path.isfile(full_path):
                with open(full_path, 'rb') as fh:
                    sub = self.process_file(fh.read(), fname)
                    ret.append(sub)

            elif os.path.isdir(full_path):
                sub = self.process_dir(full_path)
                ret.append(sub)
        return ret
249167
async function main(inputDir) {
  const index = new Index();
  try {
    const files = await readdir(inputDir);

    // Get a list of all files in the directory (filter out other directories)
    const allImageFiles = (await Promise.all(
      files.map(async file => {
        const filename = path.join(inputDir, file);
        const stats = await stat(filename);
        if (!stats.isDirectory()) {
          return filename;
        }
      })
    )).filter(f => !!f);

    // Figure out which files have already been processed
    let imageFilesToProcess = (await Promise.all(
      allImageFiles.map(async filename => {
        const processed = await index.documentIsProcessed(filename);
        if (!processed) {
          // Forward this filename on for further processing
          return filename;
        }
      })
    )).filter(file => !!file);

    // The batch endpoint won't handle
    if (imageFilesToProcess.length > 15) {
      console.log(
        'Maximum of 15 images allowed. Analyzing the first 15 found.'
      );
      imageFilesToProcess = imageFilesToProcess.slice(0, 15);
    }

    // Analyze any remaining unprocessed files
    if (imageFilesToProcess.length > 0) {
      console.log('Files to process: ');
      await getTextFromFiles(index, imageFilesToProcess);
    }
    console.log('All files processed!');
  } catch (e) {
    console.error(e);
  }
  index.quit();
}