-1

I tried to access files with with open but the files itself located not in the same folder (since I want to access files in many different folders).

import os
path = r"C:\Users\M S I\Desktop\TA MACHINE LERANING\MODEL SKENARIO UJI 1" # here contained file "[model joblib 1 1]" until [model joblib 17 24]"
l = os.listdir(path)
for model in l:
    for Coor_X in range(1,18):
            for Coor_y in range(1,25):
                with open('model joblib ['+ str(Coor_X) +' '+ str(Coor_y) +']','rb') as f:
                    print(f)

But it keeps telling "[Errno 2] No such file or directory: 'model joblib [1 1]'"

  • Your comment says you have a file named `[model joblib 1 1]` and yet and you're trying to open a file named `model joblib [1 1]`. – blhsing Sep 06 '22 at 05:49
  • sorry in that comment i mean the folder contained model joblib [1 1] instead of [model joblib 1 1] – Rizky Alvian Sep 06 '22 at 06:25
  • 1
    The path used in *open()* is relative to your current working directory. You probably need to construct an absolute path. Also, when the *open()* succeeds, *print(f)* will almost certainly not give you the results you expected – DarkKnight Sep 06 '22 at 06:28

1 Answers1

-4
with open(path, 'w') as f:
    f.write(data)
cottontail
  • 10,268
  • 18
  • 50
  • 51
Faisal Imran
  • 71
  • 1
  • 2