Python Forum

Full Version: How to stop the reading of files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need help with stopping a code from reading files from a folder. I can get my code to read all files starting from the one I assign, but I don't know how to stop the reading of the files as there are files that I don't need. I need to create an array so the indexes should be the same size, i.e. I should be able to somehow dismiss part of the files in the folder in order to have the array working. So I need help on how to break the reading of the files and start it again (so read f.ex. files number 3-5 and then 8-9 and skip 6-7).

The code in itself is this:

scan_number = np.array([1])
file_start = 20
file_folder = '\\\\location of folder in my computer\\mca_{:05}'

sample_data = fn.Sum_import_data(file_start, scan_number, n_pts, n_chnl, file_folder)
With the operating function being this

def Sum_import_data(file_start, scan_number, n_pts, n_chnl, file_folder):
     output = np.empty([n_chnl, n_pts])
     mcadata = np.empty([n_chnl, np.size(scan_number)])
     for i in np.arange(0, n_pts):
         current_mca = file_start + (scan_number - scan_number[0])*n_pts + i
         for j in np.arange(0, np.size(current_mca)):
             mcadata[:,j] = np.loadtxt(file_folder.format(current_mca[j]))
         output[:,i] = np.sum(mcadata, axis=1)
     return(output)
So I would have to be able to read the files from file 20 to file 60 and 80 to 220 in the folder which contains files from 0 to 281, but I don't know how to stop the reading of these files at a specific file and start again. I don't know if it would be possible to break the reading of the file in the file_start command line or if I should modify the function itself as it now adds (current_mca line) the next files automatically without a stop. Any help is much appreciated!
^Help still needed!
I'm not sure to understand the problem well but apparently, the function was not designed to allow you to do what you want to do, so you should probably write a modified version of the function. What makes you hesitate to write your own version?
Hey, I've been trying to change the function but the results are very complicated if I add a mca_stop parameter there, so I guess what I've been wondering if it there is an easy way to stop the reading of files. I might have worded my question badly: I have a folder in which I have a lot of files, and I tell the code which file it should start reading the files from. The problem is that in some folders not all files after the 1st one I wish to read should be read by the code, so I want to include, say, files 3-10 but not 11-14 if they don't correspond to the experiment, and then again files 15-17. However I can only assing one mca_start number (or make different parameters for each which makes the code much more complicated), so what I'm thinking is whether there is a possibility to stop the reading of the files without changing the function. split() doesn't seem to do the trick, and I couldn't find any similar examples online on how to deal with this; basically, is it mandatory to change the function, or is it possible to change the mca_start command line and tell the code to start at a certain file, stop and start again.
It is not exactly the way this function works. Here you have n points and for each of these n points the function reads the same number of files, ie the size of the scan_number array. If you are breaking this structure, the function needs to be completely redesigned. So it's up to you to see if you can adapt this algorithm
initialize the output array
initialize a temporary array
for each of the n points:
    for each file corresponding to this point:
        update the temporary array by reading the file
    sum the temporary array
    update the output array with the sum
return the output array
The key points of the algorithm are to determine which files you want to read for each point and to manage the size of the temporary array, especially if you want a different number of files for each point.