简体   繁体   中英

Python script iterates over whole folder but skips files in the folder

I tried running the following code. The code should read hdf5 files from a directory and create for every hdf5 file a png and a txt file with the same name (btw. I need it as input for the CNN YOLO). The code does what I described but only for 20 images! I added print(i) to see if the for-loop is working proper... and it is. It prints every file in the directory (over 200 files). But it just creates 20 .png and 20 .txt files.

def process_fpath(path1, path2):
    sensor_dim = (101, 101)
    onlyfiles = [f for f in os.listdir(path1) if isfile(join(path1, f))]

    for i in onlyfiles:
        if i.endswith(".hdf"):
            print(i)
            #cut ".hdf"
            name = str(i[0:-5]) 
            # create png
            im = h5py.File(path1 + str(i), 'r')
            labels_im = im['labels']
            image = im['image']
            plt.imsave(path2 + name + '.png', image)
            
            # create txt
            exp = np.column_stack((np.zeros(np.size(labels_im,0)) , labels_im[:,0]/sensor_dim[0], labels_im[:,1]/sensor_dim[1], labels_im[:,3]/sensor_dim[0], labels_im[:,3]/sensor_dim[0]))
            np.savetxt(path2 + name + '.txt', exp, delimiter = '  ', fmt=['%d', '%8f', '%8f', '%8f', '%8f'])

            continue
        else:
            continue

This is my first post so if something isn't proper please let me know.

  1. Maybe it's because of the name variable? You remove 5 characters but you want to remove only 4: name = str(i[0:-4])
  2. Not related to your question, the last 3 lines are useless. you can remove them.
            continue
        else:
            continue
  1. Try to run on a given file that is not working to understand what the problem is instead of looping on each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM