Forums

how to read a csv file that is stored in the folder on the pythonanywhere server

Hi,

Iam trying to read a csv file into python using pandas. However since iam working on a terminal i cannot store a file on a local disk and refer to that path. Therefore i though if i store the file in a directory in the pythonanywhere server, i can refer to that path.

But how to do that? What is the path for that?

Hope someone can help me out..

Regards,

Manders

If you upload it via the files tab, you can look at the top left of the page to see where you are. So it would be something like /home/manderssenn/path/to/your/file.py

Conrad, is there a way to read from a local file (local machine) and read it into my python script? Or do we need to actually upload the file onto the pythonanywhere server first to read from it? If the second one is the only option, how do I do that? (If my app is designed to receive user input (other than myself) via file from their local machine. I simply want to read the content of their file thats all.

Have a look at this Stack Overflow answer, maybe that's what you're looking for.

actually figured it out, looks like you have to use a temp path, thanks!

@Vojtaripa Can you share please?

How did that work?

@umen and @matttebbetts

     #ALLOWS TO TEMPORARILY STORE FILES 
     import tempfile

      # Input file: get the name of the file
      file = request.files['input_file']

      if not file:
          return "No file"

     #This will be the path to the file
      tempfile_path = tempfile.NamedTemporaryFile().name

     #now save it
      file.save(tempfile_path)

      # Put input file in dataframe ( read from the temp path )
      sheet = pd.read_csv(tempfile_path)

...