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

How to open a file using Numpy's genfromtxt but specifying only part of the directory path?

I would like to open a file located in the directory a/b/foldername_12345678 using genfromtxt. The following works for me

file = np.genfromtxt("a/b/foldername_12345678/filename")

However, I don’t want to keep track of the numbers at the end of the directory path in which the file is located (they are uniquely associated with "foldername"). In other words, I would like to open the file without specifying the full directory path. In linux, I can do this via e.g.

cat a/b/foldername*/filename

How can I do the same with genfromtxt?

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

>Solution :

You can use wildcards with the glob library.

import numpy as np
import glob

filepath = glob.glob("a/b/foldername*/filename")[0]
file = np.genfromtxt(filepath)

The 0 indexing assumes that there is only one possible file that will be found using this wildcard.

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