File path name for NumPy's loadtxt()

37,757

Solution 1

This is probably not a loadtxt problem. Try simply

f = open("/Users/groenera/Desktop/file.csv")

to make sure it is loadtxt's fault. Also, try using a Unicode string:

f = open(u"/Users/groenera/Desktop/file.csv")

Solution 2

You might have forgot the double slash, "//". Some machines require this.

So instead of

FH = loadtxt("/Users/groenera/Desktop/file.csv")

do this:

FH = loadtxt("C:\\Users\\groenera\\Desktop\\file.csv")
Share:
37,757

Related videos on Youtube

astromax
Author by

astromax

I'm currently a Software Engineer. I'm also an astrophysicist with interests including the formation and structure of dark matter in galaxy clusters and gravitational lensing.

Updated on June 12, 2020

Comments

  • astromax
    astromax almost 4 years

    I was wondering if somebody had some information on how to load a CSV file using NumPy's loadtxt(). For some reason it claims that there is no such file or directory, when clearly there is. I've even copy/pasted the full path (with and without the leading / for root), but to no avail.

    from numpy import *
    FH = loadtxt("/Users/groenera/Desktop/file.csv")
    

    or

    from numpy import *
    FH = loadtxt("Users/groenera/Desktop/file.csv")
    

    The documentation for loadtxt is very unhelpful about this.

    • DSM
      DSM almost 11 years
      Unrelated, but: from numpy import * -- don't do this. It'll lead to very hard-to-diagnose bugs in your code because many builtin functions will be shadowed by similarly-named numpy versions with dangerously different behaviour in certain cases. Either import functions explicitly from numpy import loadtxt or use the standard abbreviation, import numpy as np and then np.loadtxt.
    • rajpy
      rajpy almost 11 years
      Can you copy 'file.csv' file current working directory and try out. From the documentation, I don't think so you can use file path.
    • Warren Weckesser
      Warren Weckesser almost 11 years
      Show the result of the following when run in a terminal: cd /Users/groenera/Desktop; pwd; ls -l
    • astromax
      astromax almost 11 years
      @DSM Good advice about the potential namespace conflicts.
    • astromax
      astromax almost 11 years
      @rajpy I've done so and it works when the script and the csv file are in the same directory, but this is not what I want. The reason why I'm so insistent on it being in a different directory is because my script outputs data files of its own which need to be separate from the location I've placed my data files.
    • astromax
      astromax almost 11 years
      @Warren Weckesser That's the first thing I tried. The directory exists and the files are there.
    • Warren Weckesser
      Warren Weckesser almost 11 years
      "Trust, but verify." I understood that's what you said; I just wanted to see it for myself. Have you tried the suggestion in @askewchan's answer?
    • astromax
      astromax almost 11 years
      I just tried it. Although my problem was that I'm importing many modules in a bad way (using from MODULE import *). I believe there is a loadtxt namespace clash, so what fixed it for me was to do: import numpy as np (then make sure I'm using np.loadtxt() explicitly).
    • Warren Weckesser
      Warren Weckesser almost 11 years
      scipy's loadtxt (i.e. scipy.loadtxt) is just an alias for numpy.loadtxt (i.e. they are the same thing), so I don't see how using the name from scipy would have caused the problem.
  • astromax
    astromax almost 11 years
    The open command seems to work. On a completely different note, though, importing numpy loadtxt like so: import numpy as np f = np.loadtxt('/Users/groenera/Desktop/file.csv') seemed to do the trick. As far as I knew there wasn't a function in the standard library called loadtxt. I think the problem was that SciPy has a loadtxt function, and since I was importing everything from both I wasn't paying attention to which one was being called (and assuming that it was NumPy's version). For the future is there a way to test where a function is being called from in a situation like this?
  • askewchan
    askewchan almost 11 years
    @astromax scipy.loadtxt is numpy.loadtxt, so I'd be very surprised if that was the problem (scipy imports numpy functions and provides them). You can see what file it comes from with <function>.__module__. So, for me np.loadtxt.__module__ returns 'numpy.lib.npyio' and so does scipy.loadtxt.__module__
  • astromax
    astromax almost 11 years
    Uh oh. Well, it's entirely possible that I just had an error in the path (it is a pretty long path). But from what I remember importing numpy differently is what fixed it for me. Thanks for all of the help, though!
  • askewchan
    askewchan almost 11 years
    @astromax Hm.. It's also possible that some other system state was screwed up somehow, and the good old restart/reload is what fixed it.