简体   繁体   中英

Importing specific files using glob in Python

I have the some files and I want to import them into specific arrays based on a part of their name.

dark_1s-001.fit
dark_1s-002.fit
dark_1s-003.fit

dark_5s-001.fit
dark_5s-002.fit
dark_5s-003.fit

My only real question is how I should change the arguments from glob.glob('*.fit') to discern between the '1s' and '5s' file attribute.

files_1s = glob.glob('*.fit')
files_5s = glob.glob('*.fit')

darks_1s = []
darks_5s = []

for f1, f2 in zip(files_1s, files_5s):
    darks_1s.append(fits.getdata(f1))
    darks_5s.append(fits.getdata(f1))

darks_1s = np.array(darks_1s)
darks_5s = np.array(darks_5s)

median_dark_1s = np.median(darks_1s, axis=0)
median_dark_5s = np.median(darks_5s, axis=0)

You can sandwich specific pieces of the file names that must exist into the glob string.

Try this:

files_1s = glob.glob('*1s-*.fit')
files_5s = glob.glob('*5s-*.fit')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM