Daniel Weigel
Daniel Weigel

Reputation: 1137

How do I load a file with 'loadtxt ?'

Sorry for this beginner question, but...I'm a Python beginner. Still, I can't seem to find a proper answer for loadtxt not 'finding my file'...

import os
print(os.getcwd())

returns, I suppose, my current working directory.

In this case:C:\Users\danie\Desktop\python

So, when I place my csv file in it and run:

import numpy as np
dataset=np.loadtxt('Desktop/python/pima-indians-diabetes.csv', delimiter=",")

I still get

OSError: Desktop/python/pima-indians-diabetes.csv not found.

I have tried relative paths, absolute paths, f=open(..), paths with '/' and paths with '\' or with '\'...but nothing seems to make it work.. Any ideas ?

**RESOLVED: I tried Max L's hint: print(os.listdir(os.getcwd())) and I saw the list of files in my current directory:...'pima-indians-diabetes.csv.csv' ....turns out I had put the csv extension on the file name myself **

Upvotes: 0

Views: 3143

Answers (1)

Max L
Max L

Reputation: 126

If your working directory is C:\Users\danie\Desktop\python, that means that is where Python will start to look for files to import when using a relative path.

What is a relative path? It's the path to the file you want, relative to your current working directory. If a file is in the same directory, no prefix should be needed so it should just be

np.loadtxt('pima-indians-diabetes.csv', ... 

Upvotes: 2

Related Questions