Skip to content Skip to sidebar Skip to footer

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)

Solution 1:

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))

Solution 2:

You can do this:

import os.pathos.path.exists(file_path)

It will return False or True.

Solution 3:

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

Post a Comment for "How To Skip A File If It's Not There In The Directory?"