Else doesn't loop back

I have a code that opens a file, calculates the median value and writes that value to a separate file. Some of the files may be empty, so I wrote the following loop to check that the file is empty, and if so, skip it, increase the number and go back to the loop. It does what is expected for the first empty file that it finds, but not the second. Cycle below

t = 15.2
while t>=11.4:
 if os.stat(r'C:\Users\Khary\Documents\bin%.2f.txt'%t ).st_size > 0:  
    print("All good")
    F= r'C:\Users\Documents\bin%.2f.txt'%t 
    print(t)  
    F= np.loadtxt(F,skiprows=0)
    LogMass = F[:,0]
    LogRed =  F[:,1] 
    value = np.median(LogMass)  
    filesave(*find_nearest(LogMass,LogRed))
    t -=0.2
 else:
    t -=0.2 
    print("empty file")  

The output is as follows:

All good
15.2
All good
15.0
All good
14.8
All good
14.600000000000001
All good
14.400000000000002
All good
14.200000000000003
All good
14.000000000000004
All good
13.800000000000004
All good
13.600000000000005
All good
13.400000000000006
empty file
All good
13.000000000000007
Traceback (most recent call last):
  File "C:\Users\Documents\Codes\Calculate Bin Median.py", line 35, in <module>
    LogMass = F[:,0]
IndexError: too many indices

The second problem is that tsomehow it goes from one decimal place to 15, and the last place seems to increase it? Thanks for any help.

EDIT The error IndexError: too many indicesseems to apply only to files with one example line ...

12.9982324  0.004321374

, , - , ?

EDIT

, , numpy , .

In [8]: x = np.array([1,3])

In [9]: y=x[:,0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-50e27cf81d21> in <module>()
----> 1 y=x[:,0]

IndexError: too many indices

In [10]: y=x[:,0].shape
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-e8108cf30e9a> in <module>()
----> 1 y=x[:,0].shape

IndexError: too many indices

In [11]: 
0
4

try/except blocks. - :

t = 15.2
while t >= 11.4:
    F= r'C:\Users\Documents\bin%.2f.txt'%t 
    try:  
        F = np.loadtxt(F,skiprows=0)
        LogMass = F[:,0]
        LogRed =  F[:,1] 
        value = np.median(LogMass)  
        filesave(*find_nearest(LogMass,LogRed))
    except IndexError:
        print("bad file: {}".format(F))
    else:
        print("file worked!")
    finally:
        t -=0.2

. .

, , base10. :

In [13]: .3 * 3 - .9
Out[13]: -1.1102230246251565e-16
+1

, ndmin np.loadtxt ( ):

np.loadtxt('test.npy',ndmin=2)
# array([[ 1.,  2.]])
+1

Python if while for.

, if else while.

" " " "

0

ajcr , , ndim=2 numpy.loadtxt(), , .

0

Source: https://habr.com/ru/post/1659407/


All Articles