Open file knowing only a part of its name

后端 未结 4 913
轻奢々
轻奢々 2021-01-05 04:52

I\'m currently reading a file and importing the data in it with the line:

# Read data from file.
data = np.loadtxt(join(mypath, \'file.data\'), unpack=True)
         


        
4条回答
  •  情话喂你
    2021-01-05 05:36

    Try

    import os
    
    [os.path.join(root, f) for root, _, files in os.walk(mypath)
                           for f in files
                           if f.startswith('file') and f.endswith('.data')]
    

    It'll return a list of all files file*.data, in case there are more than one. You can just iterate through them. If there is only one file, then just put [0] at then end of the list comprehension.

提交回复
热议问题