python - 在继续执行剩余代码之前检查所有文件是否都在目录中?
问题描述
在代码继续开始计算之前,我试图确保所有文件(在我的情况下为'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'
这就是我到目前为止所尝试的,但没有得到想要的结果。任何帮助将不胜感激。
解决方案
好吧,感谢大家的帮助,但我设法通过使用另一种方法自己解决了这个问题。所以我认为最好分享我是如何解决它的。
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
推荐阅读
- c# - 无效 JSON 原始绘制的数据表问题
- google-maps - 地理编码 api 有多少请求是免费的?
- excel - 如何遍历高级筛选器中的条件?
- testing - 西葫芦 - 运行测试的问题
- kubernetes - 在 kubernetes 中使用 RDS 而不是 mysql 容器
- c# - 当没有检查的异常时如何在 C# 中预测异常
- c# - String.Format 为相同的代码返回不同的枚举值。如果我在枚举中添加超过 45 个字段
- wordpress - 我想更改礼品卡文字我应该如何覆盖它
- java - Spring boot项目如何保存DB用户名和密码
- javascript - awaitMessages 在我运行它时没有接受我的输入。我正在尝试使用 node.js 和 discord.js 制作一个不和谐的机器人