Python Forum
Error With Reading Files In Directory And Calculating Values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error With Reading Files In Directory And Calculating Values
#1
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?
Reply
#2
you probably mean pathname = os.path.join(dir, filename)
Reply
#3
(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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 398 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  xlwings error when reading a workbook Mishal0488 1 1,036 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  change directory of save of python files akbarza 3 806 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Coding error. Can't open directory EddieG 6 1,062 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,070 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,568 May-07-2023, 12:33 PM
Last Post: deanhystad
  Read directory listing of files and parse out the highest number? cubangt 5 2,250 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  I have an issue with Netmiko Error reading SSH protocol banner omarhegazy 2 3,511 May-16-2022, 06:05 PM
Last Post: omarhegazy
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,624 May-11-2022, 07:12 AM
Last Post: normanwolf
  How to save files in a separate directory Scordomaniac 3 1,755 Mar-16-2022, 10:17 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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