I have a folder (call it F
) with subfolders with names formatted as yyyy.doy (example 2020.001 for January first 2020). in each of the sub folders are file for multiple instruments. each file designates which instrument it is for in the 16th, 17th, and 18th character( a file fro instrument 1 would have a file as xxxxxxxxxxxxxxx001xxxxx). I am trying to create lists of file names and paths for each instrument using a python script.
If I was only looking in one of the subfolders I could use:
filelist=[]
for name in glob.glob('2020.001/*.001*'):
print(name)
filelist.append(name)
for the first instrument. I however would like to search all of the subfolders. From what I have found online I cannot use glob's recurse subfolder search to do this as my python version is 2.7. What I would like to do instead is use fnmatch
in the format below (copied somewhat from another stack overflow page) but specifically searching those 16th, 17th and 18th characters.
filelist = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(folderpath)
for f in fnmatch.filter(files, [16:19]=='.001')]
the last part of this code (files, [16:19]=='.001')
seems to fail on syntax. Can anyone help me reformat the code I have above or suggest a better way to create my lists? Keep in mind the lists will need to contain the full file path and name for each of the files but separated instrument.
You don't need fnmatch.filter
to do this, just a relatively basic Python os.path
utility to split the file name split apart from the file's extension, and a conditional generator expression.
Here's what I mean.
import os
dirpath = './instrument_files'
for dirpath, dirnames, files in os.walk(dirpath):
for f in (file for file in files
if os.path.splitext(file)[1].startswith('.001')):
print(f)