简体   繁体   中英

How do I read a .txt file from a different directory in python?

I am trying to read clients.txt from a different directory. I have a clients.txt saved on a desktop folder called "flaskpractice" and the path is (C:\\Desktop\\flaskpractice), how can I read that file that is located in a different path?

from flask import Flask, jsonify
import json

app = Flask(__name__)

@app.route("/")
def index():
    with open('clients.txt') as json_file:
            data = json.load(json_file)

    return jsonify(data)

You can simply give the absolute file path.

with open('C:/users/user/desktop/flaskpractice/clients.txt') as json_file:

The above code is just an example you have to replace the file path with the real absolute file path on your computer.

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