Python Forum
Error With Reading Files In Directory And Calculating Values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error With Reading Files In Directory And Calculating Values (/thread-24461.html)



Error With Reading Files In Directory And Calculating Values - chascp - Feb-15-2020

I'm trying to run the following code which carries out some calculations on files I have data for with functions for CentreOfMass and SED.
directory = ["./nrl169","./nrl177"]    


frame = list()
J = 1
sed = list()

for dir in directory:
    for filename in os.listdir(dir):
        pathname = os.path.join("directory", filename)
        if filename.endswith(".dump"):
            f = open(pathname, 'r')
            text = f.read()
            for line in f:
                if J>1:
                    frame = np.array(frame)
                    COM = CentreOfMass(frame, N)
                    print(COM)
                    sed.append(SED(COM))
                    frame = list()
                J = J + 1
            continue
        else:
            continue

        f.close()

    plt.hist(sed,bins=25,histype="step",density=True)
plt.show()
However, when I run this I get the following error:

Traceback (most recent call last):
File "SED.py", line 79, in <module>
f = open(filename, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'equil_T9.dump'
To explain clearly what I want to do - I am looking to calculated the SED function I have for each of the files I have contained the the two directories listed above. I then want to plot a histogram of these values. Can anyone help me with the error i'm getting and tell me if i'm on the right track?


RE: Error With Reading Files In Directory And Calculating Values - Gribouillis - Feb-15-2020

you probably mean pathname = os.path.join(dir, filename)


RE: Error With Reading Files In Directory And Calculating Values - chascp - Feb-15-2020

(Feb-15-2020, 01:54 PM)Gribouillis Wrote: you probably mean pathname = os.path.join(dir, filename)
Thank you I just realised this but now have a second error regarding an array.

it reads Traceback (most recent call last):
File "SED.py", line 86, in <module>
COM = CentreOfMass(frame, N)
File "SED.py", line 17, in CentreOfMass
init=frame[:,-1]
IndexError: too many indices for array

relating to a section of code as follows:

[python]
def CentreOfMass(frame, N):
init=frame[:,-1]
include_types = np.nonzero(init==41)
refor_types=include_types[0][-1]
frame = frame[:refor_types, :]
limit_frame = frame[(frame[:,-1]<41),:]
mol = len(set(limit_frame[:,-3]))
[\python]