Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Glob pattern to read in a specific string in Python

I have a folder with a list of files I want to read in as a list of full file paths.

The files that I want to read in have this structure: [0-9]_beta_[Y].nii.gz, where:

  • [0-9] is a 3 digit sequence of numbers (e.g. 123)
  • [Y] is a character string of any length (e.g. ‘faces’, ‘faces_up’)

What is the right pattern for this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the code I have so far but it doesn’t get all the files I need:

file_list = glob.glob(os.path.join(data_dir, f'*_beta_*nii.gz'))

Thanks so much for your help!

>Solution :

Try:

file_list = glob.glob(os.path.join(data_dir, '[0-9][0-9][0-9]_beta_*.nii.gz'))

[0-9][0-9][0-9] matches exactly 3 digits, your * matches anything. And you should have . before nii.gz.

The string doesn’t need to be an f-string, you’re not substituting anything into it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading