astroluv
astroluv

Reputation: 793

How to skip a file if it's not there in the directory?

So here i'm reading a fits file.

path = "/home/Desktop/2d_spectra"
for filename in os.listdir(path):
   if filename.endswith("_e2ds_A.fits"):
      e2ds_hdu = fits.open(filename)
      e2ds_header = e2ds_hdu[0].header
      blaze_file = e2ds_header['HIERARCH ESO DRS BLAZE FILE']
      date = e2ds_header['DATE-OBS']
      date2 = date = date[0:19]
      bis_file = glob('HARPS.' + date2 + '*_bis_G2_A.fits')
      ccf_table = glob('HARPS.' + date2 + '*_ccf_G2_A.tbl')
      filenames = {'filename', 'blaze_file', 'bis_file', 'ccf_table'}
      all_exist = filenames.issubset(os.listdir(path))

Now i want to make sure that my script does the next part of the computations only if all the four files (filename, blaze_file,bis_file,ccf_table) defined above are there in the directory as some of the files are not in the folder and because of that it gives the error : "No such file or directory".

      blaze_hdu = fits.open(blaze_file)
      blaze = blaze_hdu[0].data
      data_cor = data/blaze

      bis_hdu = fits.open(bis_file[0])
      bis_header = bis_hdu[0].header
      berv = bis_header['HIERARCH ESO DRS BERV']                         
      rv   = bis_header['HIERARCH ESO DRS CCF RV']
      rvn  = bis_header['HIERARCH ESO DRS CCF NOISE']

      df=pd.read_table(ccf_table[0],skiprows=2,usecols=(0,4),names=['order','rv'],)
      df=df.to_dict(orient='dict')

      df = df['rv']
      for i in np.arange(0,72,1):
        ll = wave[i]
        flux = data_cor[i]

      tmpFile = 'order_'+str(i)+'.txt'
      path =  '/home/gyanender/bin/ARES/'+tmpFile
      with open(path, 'w') as f:
         writer = csv.writer(f, delimiter=' ')
         writer.writerows(zip(ll,flux))


      mine_opt =  '/home/gyanender/bin/ARES/mine.opt'
      file_opt=open(mine_opt,'w')
         file_opt.writelines(("specfits='order_"+str(i)+".txt'","\n","readlinedat='linelist.dat'","\n",\
         "fileout='txt_"+str(i)+".ares'","\n","lambdai=3600.","\n","lambdaf=9000.","\n","smoothder=4","\n",\
         "space=3.0","\n","rejt="+str(SN_dic[i][0]),"\n","lineresol=0.1","\n","miniline=1","\n",\
         "plots_flag=0","\n","rvmask='0,0'","\n"))
      file_opt.close()

      working_dir = '/home/gyanender/bin/ARES'               
      subprocess.check_call(['./ARES'], cwd=working_dir)

enter image description here

So what could be the best way to make sure that i get the desired result.

Upvotes: 0

Views: 1452

Answers (3)

Agile_Eagle
Agile_Eagle

Reputation: 1839

You can do this:

import os.path
os.path.exists(file_path)

It will return False or True.

Upvotes: 0

Jerub
Jerub

Reputation: 42608

To test that 4 files exist in a directory, you can do this:

filenames = {'filename1.ext', 'filename2.ext', 'filename3.ext', 'filename4.ext'}
all_exist = filenames.issubset(os.listdir(path))

Upvotes: 2

Abhishek Arya
Abhishek Arya

Reputation: 500

Use os.path.exists(path/to/file) before opening it.

Upvotes: -1

Related Questions