Question
Python: traceback Error FileNotFoundError: [Errno 2] No such file or directory I am getting an error where I cannot skip a missing file in the
Python: traceback Error FileNotFoundError: [Errno 2] No such file or directory I am getting an error where I cannot skip a missing file in the series and read past it. For example, I have file out_1890_0003_z1.dat, but not out_1890_0004_z1.dat thus it gives the error that reads: Traceback (most recent call last): File "abundance_distribution.py", line 33, in with open(file) as ab: FileNotFoundError: [Errno 2] No such file or directory: 'out_1497_0003_z1.dat' I posted the entirely below; you may notice that I am reading through a directory of files or a file in hopes to plot out the abundances. A sample of what I use for testtime.dat: 5.80212e+08 -3.77103e+09 1.27941e+09 -3.92622e+09 9.46607e+08 -3.88296e+09 1.03484e+09 -4.25997e+09 2.09741e+09 -4.17192e+09 2.15069e+09 -5.29324e+09 3.19712e+09 -4.65345e+09 5.28521e+08 -2.96046e+09 5.70772e+08 -3.53496e+09 5.38630e+08 -3.57978e+09 3.98422e+08 -3.46427e+09 3.37453e+08 -3.57777e+09 1.16953e+09 -3.02742e+09 5.87569e+08 -3.19243e+09 9.60321e+08 -3.12258e+09 1.10736e+09 -3.38792e+09 9.20522e+08 -3.01107e+09 1.13096e+09 -3.11983e+09 1.04215e+09 -3.22943e+09 1.02869e+09 -3.11037e+09 8.26629e+08 -3.61914e+09 1.16423e+09 -3.54326e+09 1.14112e+09 -3.48113e+09 5.69292e+08 -2.41511e+09 5.90233e+08 -2.48461e+09 5.45103e+08 -2.64106e+09 1.90860e+08 -2.55949e+09 4.61044e+08 -2.42837e+09 3.57791e+08 -2.87573e+09
This is a sample of the out_whateveryouit_0000_z1.dat files I use in the section I have an error in (it is in bold), the files are all listed in a directory, here is one of many thousands of files: * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 1 4.797030000000E-05 9.000000E-30 1.000000E-29 1.100000E-29 1.100000E-29 5.000000E-01 1.300000E-29 1.400000E-29 1.200000E-29 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 101 6.993032347891E-02 9.083043E-23 2.079933E-15 2.610734E-13 2.697450E-14 4.895700E-01 5.511230E-08 4.293999E-09 2.025953E-22 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 201 7.039261757564E-02 6.396873E-15 7.006367E-17 7.696272E-15 3.277727E-09 2.758807E-04 1.518446E-08 9.449274E-13 3.550145E-16 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 301 7.088481182816E-02 4.591306E-12 5.769626E-16 1.187112E-15 1.667039E-10 1.526322E-04 1.371243E-10 3.865183E-14 9.752786E-16 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 401 7.134944609308E-02 2.557561E-11 1.978589E-13 2.380623E-14 1.197408E-10 2.965548E-05 4.000992E-10 5.180620E-13 2.933697E-15 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 501 7.176816605883E-02 5.623970E-11 1.903319E-12 1.042387E-13 6.951315E-11 1.450983E-05 6.664600E-10 2.089877E-12 1.731502E-15 * * trho_hist: mass interior = 0.000E+00 shell mass = 0.000E+00 * time b9 b10 b11 c11 c12 c13 c14 n12 601 9.292677737926E-02 3.137908E-12 3.939535E-14 4.051370E-16 2.984251E-12 1.811906E-06 1.223778E-11 1.150333E-14 4.111200E-17 Finally, this is the entire code I have been working on, and I feel outside of the section of code I have highlighted, all else is working fine. The bolded part of the code is what I am working on. I would appreciate any help with pointing out issues in my code. Thanks. ###This script can be used to plot the pos_x and pos_y and then ###scheme the particles by their abundances distribution.
import numpy as np import matplotlib.pyplot as plt import glob as glob
plt.style.use('dark_background')
# to read the pos_ and pos_y with open('testtime.dat', 'r') as f: lines = f.readlines() size=len(lines) x = [float(line.split()[0]) for line in lines] # will read the column one y = [float(line.split()[1]) for line in lines] # will read the column two s=[3] # the size of the particle on the plot x = np.array(x) #print(x) y= np.array(y) color_array=[]
c12_app= [] #empty list to stor the c12 o16_app= [] #empty list to stor the o16 si28_app= [] #empty list to stor the si28 #print(size) # to read the abundances and shade the particles for file in os.listdir('/work/07422/tg867218/stampede2/test'):#Reads where the file is in the directory if file.endswith("_z1.dat"):#This should read the file in the folder that ends with .dat with open(file) as ab: file = os.listdir('_z1.dat') abundance = f.readlines() for file in files: if files.strip()[0].isdigit():#This separates the files be useful data abd splits off anything that is not cols = files.strip().split() #print('file', file) # To append the atom. baryon, and mass fraction to empty lists atom_number=[] # to append the atomic number from the abundance files baryon_number=[] # to append the baryon number from the abundance files mass_fraction=[] # to append the mass fraction from the abundance files # this loop to read the columns from the abundance files for w in abundance: line = w.split() atom_number.append(int(line[0])) baryon_number.append(int(line[1])) mass_fraction.append(float(line[2])) #print ('ato', atom_number) #print ('bar', baryon_number) #print ('mass', mass_fraction) #print ('isotope', isotope_symbols) # to calssfiy the baryon number to empity lists according to the colors that we want green_app= 0 blue_app= 0 red_app= 0 white_app= 0
for j in range (229): # 229 is the number of the line in every abundance file #The white color represents 56Ni, and the green, blue, and red colors in #the figure represent isotopes with A <= 16, intermediate-mass isotopes with 16 < A <= 40, #and iron-peak isotopes for all A > 40 (except 56Ni) respectively if(baryon_number[j] <= 16): green_app += mass_fraction[j] elif (baryon_number[j] > 16 and baryon_number[j] <= 40): blue_app += mass_fraction[j] elif (baryon_number[j] > 40 and (atom_number[j] != 28 or baryon_number[j] != 56)): red_app += mass_fraction[j] else: white_app = mass_fraction[j] # To find the max abundances for C12, O16, and Si28 for k in range (229): if (baryon_number[k] == 12 and atom_number[k] == 6): c12_app.append(mass_fraction[k]) elif (baryon_number[k] == 16 and atom_number[k] == 8): o16_app.append(mass_fraction[k]) elif (baryon_number[k] == 28 and atom_number[k] == 14): si28_app.append(mass_fraction[k]) #print('green_app', green_app) #print('blue_app', blue_app) #print('red_app', red_app) #print('white_app', white_app) #print ('c12_app', c12_app) #print ('o16_app', o16_app) #print ('si28_app', si28_app)
color_max = np.zeros(4)
color_max[0] = green_app color_max[1] = blue_app color_max[2] = red_app color_max[3] = white_app #print('green', color_max[0]) #print('blue', color_max[1]) #print('red', color_max[2]) #print('white', color_max[3]) # Max value color_ind = np.argmax(color_max) #print('color_ind', color_ind) res_max_c12 = max(float(sub) for sub in c12_app) res_max_o16 = max(float(sub) for sub in o16_app) res_max_si28 = max(float(sub) for sub in si28_app)
if color_ind == 0: color_array.append('g') elif color_ind == 1: color_array.append('b') elif color_ind == 2: color_array.append('r') else: color_array.append('w') color_array= np.array(color_array, dtype=object) #print('color_array', color_array)
print('max_c12', res_max_c12) print('max_o16', res_max_o16) print('max_si28', res_max_si28) plt.figure(figsize=(10,20)) plt.xlabel('r(cm)', fontsize=18) plt.ylabel('z(cm)', fontsize=18) plt.title('last time step 1.9 s', fontsize=18) plt.xlim(0, 13e9) plt.ylim(-13e9, 13e9) plt.scatter(x, y, s, c= color_array) plt.savefig('afteredit')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started