How to skip over a file that does not exist when using a loop to read in multiple files at once, path or file doesn't exist

112 Views Asked by At

I am using a for loop to read in everyfile with a particular extension (.AFEPSI) that exists within multiple directories and their subdirectories.

However, the way I wrote the code, there is one file path that gets assigned that doesn't actually exist.

Instead of skipping that file that doesn't exist, it returns an error. Is there anyway of skipping over a loop when the file doesn't exist??

Here is my code

for(samp_dir in Sys.glob("/projectnb/gtex/data/*/*")){
   sample <-  basename(samp_dir)
   afepsi <- paste(sample, "AFEPSI",sep=".")
   afepsipath <- paste(samp_dir, "hit-index", afepsi, sep="/")
   afepsifile <- read.delim(afepsipath)
   print(afepsifile)
}

here is the error:

cannot open file '/project/gtex/data/tissue/tissue-bam-manifest.json/hit/tissue_bam-manifest.json.AFEPSI': Not a directory

OR:

how do i leave out certain objects (for example here, any that contain the word json) so that when i assign paths, it does not include any of the files that contain json

0

There are 0 best solutions below