1

I am trying to create an array with 2 columns and over 7000 rows (lots of data). The data I have is written as a text file and is formatted in two columns, each variable being separated by a space. My biggest problem is that NumPy can't seem to find the file.

 1.  import numpy as np
 2.  np.fromfile(stardata.txt)

This returns:

NameError: name 'stardata' is not defined

I have checked the directory and everything seems to be in order. The file is in the correct directory.

My next problem is seeing if this would make a good array. I am guessing I might have to use .reshape() to make it look the way I want it to be. After that, I will be using the data to make a (Hertzsprung-Russell) diagram.

The full error message is here:

C:\Users\Petar\AppData\Local\Enthought\Canopy\System\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:
    175                 filename = fname
--> 176             exec compile(scripttext, filename, 'exec') in glob, loc
    177     else:
    178         def execfile(fname, *where):

    C:\Users\Petar\Desktop\test.py in <module>()
          1 import numpy as np
    ----> 2 np.fromfile(stardata.txt)

    NameError: name 'stardata' is not defined
2
  • After adding the quotes around the file name, it now says that there is no such file or directory. Commented Sep 27, 2013 at 16:46
  • I would recommend against using np.fromfile to load a text file. np.genfromtxt is much improved. Commented Sep 27, 2013 at 18:09

1 Answer 1

3

Your file name needs to be wrapped in quotes:

np.fromfile('stardata.txt')
Sign up to request clarification or add additional context in comments.

5 Comments

It now says there is no such file or directory.
Where is stardata.txt in relation to the script you are executing and where you are executing it from? It sounds like you may just need the full path name in place to work.
Both the script and the text file are saved on my Desktop.
You need to be running the script from your desktop, then too. In your command window make sure you are at C:\Users\<name>\Desktop before executing python <script>.py.
When I ask it to list the directory, it states C:\Users\<name>\Desktop\<filename.py>. Maybe this is why it can't find the text file. How would I go about chaning the directory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.