Well, it's not a file but a directory, but as I understand that shouldn't make a difference? The directory does exist, and the path should be correct as it works when calculating the files in that directory in the same function, and I use the same mca_file there. The complete function is this
def function(n_pts, n_chnl, mca_file): print('Checking that n_files divided by n_pts is an integer...') n_files = 0 for path in pathlib.Path(mca_file).iterdir(): if path.is_file(): n_files += 1 if n_files % n_pts == 0: print('Done!') else: print('Not an integer, check the number of files in your mca folder') raise SystemError print('Summing up mcas in mca_file...') mcadata = np.zeros([n_chnl, n_pts]) for energy_point in range(n_pts): current_mca = os.listdir(mca_file) for mca in current_mca: filename = mca_file.format(mca) print('DEBUG: is file? ', os.path.isfile(filename), filename) mcadata[:, energy_point] += np.loadtxt(filename) #mcadata[:, energy_point] += np.loadtxt(mca_file.format(mca)) print('Done!') return mcadataand the code does the first part well, dividing n_files with n_pts using the same exact directory named as 'mca_file'. Perhaps the os.listdir command doesn't work here, then? Also, huge thanks for your help so far!