Python Forum
How to stop the reading of files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to stop the reading of files
#1
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!
Reply
#2
^Help still needed!
Reply
#3
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?
Reply
#4
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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading Multiple text Files in pyhton Fatim 1 1,911 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Trouble reading files using pyexcel codebeacon 2 2,182 Feb-08-2021, 05:53 AM
Last Post: codebeacon
  Reading txt files in ssh server. therenaydin 6 4,128 Aug-06-2020, 05:17 AM
Last Post: ndc85430
  How can I speed up my openpyxl program reading Excel .xlsx files? deac33 0 3,389 May-04-2020, 08:02 PM
Last Post: deac33
  Other modules for reading audio files? jedzz 0 1,593 Mar-25-2020, 11:07 PM
Last Post: jedzz
  Error With Reading Files In Directory And Calculating Values chascp 2 2,428 Feb-15-2020, 01:57 PM
Last Post: chascp
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,907 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Reading and writing files JakeHoward4 1 1,809 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Reading PCAP FIles Variables 5 9,869 Apr-26-2019, 06:05 AM
Last Post: buran
  Python 2.7.13 Issue Reading .txt files Properly username1145 3 2,540 Mar-24-2019, 03:08 PM
Last Post: username1145

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020