Forums

What's the right path setting to read files ?

I'm starting with pythonanywhere and I'm facing a very basic issue. Here is my code

import os
import re
import numpy as np
import pandas as pd
from flask import Flask, request

#Load data into Pandas dataframe
datafile = "movie_metadata_red.csv"
df = pd.read_csv(datafile, low_memory=False)

The file movie_metadata_red.csv is available in the root directory of my site, however I get an error FileNotFoundError I also tried with a /data/ sub directory, putting the files there, but every time I get the same message.

What should I do th have Python find my data files ?

See the help page that we have for that: http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Thanks for the hint. I could solve my issue.

Great!

I can't get rid of the FileNotFoundError error

I intent to read a simple csv file stored in my static folder.

I get the FileNotFoundError: [Errno 2] No such file or directory: 'home/smartytest/mysite/meta_scraping.csv'

My code is quite simple, but I can't realize what I am doing wrong:

1
2
3
4
5
6
7
 import pandas as pd

myfile = "meta_scraping.csv"

 url = "home/smartytest/mysite/static/" + myfile
 col_list = ['txt', 'resp', 'txt_proc', 'link']
 df_quest = pd.read_csv(url, usecols=col_list, encoding='utf-8')

You're missing a slash from the start of your variable url. The path that you're using is relative to the current working directory of your script; if you start it with "/home" instead of "home", then it will be an absolute path and should work.

Hi Team, I'm still having this issue after checking and changing the path of my URL as above: My code is quite simple, but I can't realize what I am doing wrong:

 from flask import Flask, render_template, request #os
 import pandas as pd

 app = Flask(__name__)
 import pandas as pd

 @app.route('/', methods=['GET', 'POST'])
 def index():
 return render_template('index.html')


@app.route('/data', methods=['GET', 'POST'])

def data():
if request.method == 'POST':
    file = request.form['upload-file']

    url = "/home/vojtaripa/mysite/" + file  
    col_list = ['txt', 'resp', 'txt_proc', 'link']
    df_quest = pd.read_csv(url, usecols=col_list, encoding='utf-8')
    return render_template('data.html', data=df_quest.to_html())

Error that I'm getting:

FileNotFoundError: [Errno 2] No such file or directory: '/home/vojtaripa/mysite/testCSV.csv'

It means that the file that you're trying to read does not exist. Make sure that the file that you're trying to read is actually at that path, or modify the file path so that it references a file that exists.

Thank you Glenn, I'm prompting the user to select a file from their local desktop / machine and then I would just read the data from there.

It works fine on my local machine.

I also tried various other paths here without success.. : /home/vojtaripa/mysite/ /home/vojtaripa/mysite/index/ /home/vojtaripa/mysite/data/ /home/vojtaripa/mysite/templates /home/vojtaripa/mysite/templates/index/ /home/vojtaripa/mysite/templates/data/

/user/vojtaripa/files/home/vojtaripa/mysite/ /user/vojtaripa/files/home/vojtaripa/mysite/index/ /user/vojtaripa/files/home/vojtaripa/mysite/data/ /user/vojtaripa/files/home/vojtaripa/mysite/templates /user/vojtaripa/files/home/vojtaripa/mysite/templates/index/ /user/vojtaripa/files/home/vojtaripa/mysite/templates/data/

It doesn't look like you're saving the file, so it won't be found under those paths.

had to use temp files, thanks!

def depthWellAdvanced(user_lat, user_lon, thresh):

    def create_well_recommendation_map(
        user_latitude, user_longitude, threshold_distance=float(thresh)
    ):

        file_path = "/home/varad2004/mysite/dugwell.csv"
        df = pd.read_csv(file_path)

        df = df.dropna(subset=["Y", "X", "Depth (m.bgl)", "Well Type"])

        X = df[["Y", "X"]]
        y_depth = df["Depth (m.bgl)"]
        y_well_type = df["Well Type"]

        depth_model = KNeighborsRegressor(n_neighbors=3)
        depth_model.fit(X, y_depth)

        well_type_model = KNeighborsClassifier(n_neighbors=3)
        well_type_model.fit(X, y_well_type)

        def recommend_well_and_nearest(user_lat, user_lon):
            user_location = [[user_lat, user_lon]]

            nearest_dugwell_index = depth_model.kneighbors(user_location)[1][0][0]
            nearest_dugwell_coordinates = X.iloc[nearest_dugwell_index]
            nearest_dugwell_depth = y_depth.iloc[nearest_dugwell_index]
            nearest_dugwell_well_type = y_well_type.iloc[nearest_dugwell_index]

            distance_to_nearest_dugwell = geodesic(
                user_location[0], nearest_dugwell_coordinates
            ).kilometers
            if distance_to_nearest_dugwell > threshold_distance:
                return f"No suitable well within {threshold_distance} km.", None, None

            return (
                nearest_dugwell_depth,
                nearest_dugwell_well_type,
                nearest_dugwell_coordinates,
            )

        map_center = [user_latitude, user_longitude]
        map_object = folium.Map(location=map_center, zoom_start=12)

        recommendation_result = recommend_well_and_nearest(
            user_latitude, user_longitude
        )
        recommended_depth, recommended_well_type, recommended_coordinates = (
            recommendation_result
        )

        if (
            isinstance(recommended_well_type, str)
            and recommended_well_type != "No suitable well within 3 km."
        ):

            folium.Marker(
                location=[user_latitude, user_longitude],
                popup=f"Recommended Depth: {recommended_depth} meters, Recommended Well Type: {recommended_well_type}",
                icon=folium.Icon(color="red"),
            ).add_to(map_object)

            folium.Marker(
                location=[recommended_coordinates["Y"], recommended_coordinates["X"]],
                popup=f"Nearest Well - Depth: {recommended_depth} meters, Well Type: {recommended_well_type}",
                icon=folium.Icon(color="green"),
            ).add_to(map_object)
        else:

            folium.Marker(
                location=[user_latitude, user_longitude],
                popup="No suitable well within 3 km.",
                icon=folium.Icon(color="gray"),
            ).add_to(map_object)

        return map_object, recommended_depth, recommended_well_type

    user_latitude = user_lat
    user_longitude = user_lon

    map_object, recommended_depth, recommended_well_type = (
        create_well_recommendation_map(user_latitude, user_longitude)
    )

    print(f"Recommended Depth: {recommended_depth} meters")
    print(f"Recommended Well Type: {recommended_well_type}")

    map_object.save("well_recommendation_map.html")

    with open("well_recommendation_map.html", "r") as file:

        html_content = file.read()

    result = {
        "depth": f"{recommended_depth}",
        "well_type": f"{recommended_well_type}",
        "html_content": f"{html_content}",
    }

    return result

I am trying to get the file dugwell.csv but getting an error File does not exist. My mysite directory contains these files

Aquifer_data_Cuddalore.xlsx Modified_Water_Quality_Shuffled.csv Transmisivitty.xlsx UpdatedWaterQuality.csv dugwell.csv flask_app.py

your username (and home directory) is vrd2004 not varad2004