I'm trying to read all the files in a directory and append the text in them into a list but I get the error in the header. However, the file actually exists. My code is:
pos_lines = []
train_pos_path = r'C:\Users\Halid\Desktop\nlp_p5\nlp_dir2\TRAIN\posTr'
# print(os.listdir(train_pos_path))
for filename in os.listdir(train_pos_path):
with open(filename, "r",encoding="utf-8", errors='ignore') as f:
for line in f.readlines():
pos_lines.append(line)
The path is the absolute path of the directory and print(os.listdir(train_pos_path)) gives the list of the files under that directory. However, I can't open the files in it and it gives me the error I mentioned. My code I'm trying to run is in "C:\Users\Halid\Desktop\nlp_p5". Does anybody know why I'm getting this error and how can I solve this?