简体   繁体   中英

Error "no such file or directory" when using os.chdir in Flask

I can boot the app up with Flask, it loads and displays localhost viewing info, then immediately quits with the error:

can't open file 'server.py': [Errno 2] No such file or directory

This is normally what you get when there is no file of that name in the folder, but there definitely is in this case as it successfully loads it initially

EDIT: Adding code for server.py :

from flask import Flask, jsonify
from services.controller import Controller

app = Flask(__name__)
path = '/Users/bhouwens/some/path'
ctrl = Controller(path)

@app.route('/')
def home():
    stats = {'path' : path}

    return jsonify(stats)

@app.route('/task-runners')
def task_runners():
    return jsonify({'task_runners': ctrl.task_runners})

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

As mentioned, os.chdir from within a Flask app doesn't seem possible without breaking the server.

In my case, I'm trying to read a file in another directory using another module, where the path to the directory is passed into the module as a parameter in this line:

ctrl = Controller(path)

I've discovered that the open function that Python makes available can open files in other directories, so I got around this issue by using

with open(path + '/file_to_open.txt', 'r') as file:

from the Controller module.

Hopefully this helps someone else who runs into the same problem.

Like you already discovered, you can't do chdir .

import os
from flask import Flask

app = Flask(__name__)
os.chdir('..')

app.run(debug=True)

When you execute this file, this is the output:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
/usr/bin/python: can't open file 'example.py': [Errno 2] No such file or directory

I don't know exactly what causes this issue, but if you check app.root_path after an os.chdir() , you would find it messed up, changing app.config['APPLICATION_ROOT'] doesn't solve this either, you need to change app.root_path attribute manually with the new directory path, you can do it while initializing the flask app like so app = Flask.flask(__name__, root_path=BASE_DIR) , but you can also change it at any time while flask is running.
Without doing so, Flask does this:
if your app was at ~/one/two/three/app.py and you try to run the app from ~/one , doing an os.chdir() to os.path.dirname(os.path.abspath(__file__)) inside the app would result the app.root_path to be equal to '~/one/two/three/two/three' , it basically doubles the portion of the path left between the location where you called the app and the app file itself, and if you were running the app from ~/one/two/three/four/five and doing os.chdir() to os.path.dirname(os.path.abspath(__file__)) inside the app this would result app.root_path to be equal to '~/one' ,ie truncates the number of directories it's far from the app file starting from the app.py directory and going backwards.
os.path.dirname(os.path.abspath(__file__)) equals '~/one/two/three'
Weird bug :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM