0

I'm getting error when i make postman request to my api when trying to read files from a directory.

    cwd = os.getcwd()
    print(cwd)
    cwd = cwd.replace('\\','/')
    print(cwd)
    path = cwd + "/training_data/"
    print(path)
    try:

        for files in os.listdir(path):
            data = open(path + files,'r').readlines()
            bot.train(data)

    except Exception as e:
        return jsonify("Error while opening file",path,cwd,os.listdir(path))

I'm getting the following exception:

[
"Error while opening file",
"C:/Users/RakeshS/Desktop/app/training_data/",
"C:/Users/RakeshS/Desktop/app",
[
    "code.txt",
    "deputation1.txt",
    "football.txt",
    "Greeting.txt",
    "internetaccess.txt",
    "intravels.txt",
    "sentiment.txt",
    "system.txt"
]]

Why is it not able to open the file and read data when i'm getting all the list of files inside the directory?

alia
  • 459
  • 4
  • 16
  • Did you tried to print the **actual exception?** – Abdul Niyas P M Nov 22 '18 at 14:06
  • yes getting this 'charmap' codec can't decode byte 0x9d in position 1459: character maps to ' – alia Nov 22 '18 at 14:14
  • **Why is it not able to open the file and read data when i'm getting all the list of files inside the directory?** The issue is not with the `FilePermission`. That is why you were able to list the files on the directory. The issue is with the file encoding. You should pass the encoding of the file with `open(path + files,'r', encoding="your_file_encoding")`. I will also recommend you to read [this](https://stackoverflow.com/a/36159610/6699447) answer. – Abdul Niyas P M Nov 22 '18 at 14:18

1 Answers1

1

Here is complete solution to your problem:

from flask import Flask, jsonify
import os
app = Flask(__name__)


@app.route('/')
def hello_world():
    cwd = os.getcwd()
    path = os.path.join(os.getcwd(), 'training_data')
    try:
        for file in os.listdir(path):
            path_and_file = os.path.join(path, file)
            data = open(path_and_file, 'r').readlines()
            print(data)  # To print everything from a file
        return jsonify("Files successfully opened", path, cwd, os.listdir(path))

    except:
        return jsonify("There was error opening files", path, cwd, os.listdir(path))


if __name__ == '__main__':
    app.run()

Here is the output:

Response of postman

Explanation:

In my example, I put it on / route, but you can put it where ever you want.

Whenever I go to / route, I get JSON response. os.getcwd() gets me current directory, but I join two paths using os.path.join() function. From python documentation:

Join one or more path components intelligently.

You can read more on python documentation. Next, since I get the path to training_data, I need to join again path to training_data and file. And I return JSON data. If anything goes wrong, you can print traceback in except clause and also return data, so that flask doesn't raise error for returning no response to the user.

P.S.

training_data folder is in a same level as a your flask application.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57