In the same directory I have several files, some of them are sample measurements and others are references. They look like this:
samples = 
references = 
ref = raw_input("Enter first reference name: ")
num_refs = raw_input("How many references are? ")
ref = sorted(glob.glob(ref+num_refs))
samples = sorted(glob.glob(*.dat)) not in references
You can use
glob.glob('*.dat') to get a list of all of the files and then slice that list according to your criteria. The slice will begin at the index of the first reference name, and be as large as the number of references.
Extract that slice to get your references. Delete that slice to get your samples.
import glob samples =  references =  ref = raw_input("Enter first reference name: ") # blablabla_100.dat num_refs = int(raw_input("How many references are? ")) # 3 all_files = sorted(glob.glob('*.dat')) first_ref = all_files.index(ref) ref_files = all_files[first_ref:first_ref+num_refs] sample_files = all_files del sample_files[first_ref:first_ref+num_refs] del all_files print ref_files, sample_files
['blablabla_100.dat', 'blablabla_101.dat', 'blablabla_102.dat'] ['blablabla_350.dat', 'blablabla_351.dat', 'blablabla_352.dat', 'blablabla_353.dat']