I have 10 data in txt format: a1.txt,a2.txt,...,a10.txt and I want to load them one by one in a for loop.
To load a file I use
data = read_data('a1.txt');
but I am not sure how to use it in a for loop.
I have 10 data in txt format: a1.txt,a2.txt,...,a10.txt and I want to load them one by one in a for loop.
To load a file I use
data = read_data('a1.txt');
but I am not sure how to use it in a for loop.
Search for files inside path that end with .txt
from os import listdir
from os.path import isfile, join
mypath='/tmp'
files = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f.endswith('.txt')]
Another approach with pathlib (python 3.4+). It recognizes also the a for matching:
from pathlib import Path
files = [str(f) for f in Path(mypath).iterdir() if f.match("a*.txt")]