首页 > 解决方案 > 在继续执行剩余代码之前检查所有文件是否都在目录中?

问题描述

在代码继续开始计算之前,我试图确保所有文件(在我的情况下为'ccf_table','bis_file')都存在于目录中。如果其中任何一个(bis_file,ccf_table)不在任何输入文件的目录中(在我的情况下为“文件名”),那么只需传递该输入文件。

path = "/home/Desktop/s1d_data"
for filename in os.listdir(path):
  if filename.endswith("_s1d_A.fits"):
  s1d_hdu = fits.open(filename)
  s1d_header = s1d_hdu[0].header
  date = s1d_header['DATE-OBS']
  date2 = date = date[0:19]
  ccf_table = glob('HARPS.' + date2 + '*_ccf_G2_A.tbl')
  bis_file = glob('HARPS.' + date2 + '*_bis_G2_A.fits')

  filelist = ['ccf_table','bis_file']
  while True:
    list1 = []
    for file in filelist:
      list1.append(os.path.isfile(file))

      if all(list1):
         break
      else:
         pass

  df=pd.read_table(ccf_table[0],skiprows=2,usecols=(0,4),names=['order','rv'],)
  df = df.rv.mean()
  out_name = s1d_header['OBJECT'] +'-' + s1d_header['DATE-OBS'] +'.fits'

这就是我到目前为止所尝试的,但没有得到想要的结果。任何帮助将不胜感激。

标签: pythonfits

解决方案


好吧,感谢大家的帮助,但我设法通过使用另一种方法自己解决了这个问题。所以我认为最好分享我是如何解决它的。

for filename in os.listdir("/home/Desktop/2d_spectra"):
   if filename.endswith("_e2ds_A.fits"):
     e2ds_hdu = fits.open(filename,ignore_missing_end=True)
     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]
     blaze_file1 = glob(blaze_file)
     bis_file = glob('HARPS.' + date2 + '*_bis_G2_A.fits')
     ccf_table = glob('HARPS.' + date2 + '*_ccf_G2_A.tbl')

     ll = np.size([blaze_file1]) + np.size(bis_file) + np.size(ccf_table)   #[] will convert the string to numpy array
        if ll == 3:      
        #did what i wanted to do.

        else:
           none

推荐阅读