0

I have folders containing a specific file len_J.py. I am reading every folder and calculating average for each line. But this code doesn't work when one of the folders doesn't have len_J.py. Basically, it should calculate average of folders which have len_J.py in them and skip the ones which don't.

NFILES = 3

values = [[] for _ in range(NFILES)]

for i in range(NFILES):
    with open(rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Trials\{i+1}\len_J.py") as data: 
        values[i] += map(int,filter(lambda x: x.strip().isdecimal(), data))
        
for t in zip(*values):
    print(sum(t)//len(t))
  • Does this answer your question? [How do I check whether a file exists without exceptions?](https://stackoverflow.com/questions/82831/how-do-i-check-whether-a-file-exists-without-exceptions) – Yevhen Kuzmovych Jun 13 '23 at 09:15

1 Answers1

0

You could use

os.path.isfile(/path/to/file)

And construct the file path before hand.

azerELweed
  • 67
  • 1
  • 11