简体   繁体   中英

Why can't python/jupyer notebook find the text file? Where should it be saved?

I'm trying to load a text file as an array in python by entering this code:

from numpy import loadtxt
    values = loadtxt("values.txt", float)
    mean = sum(values)/len(values)
    print(mean)

but when I run the program I get:

  OSError                                   Traceback (most recent call last)
<ipython-input-10-4b9a39f8b17f> in <module>
      1 from numpy import loadtxt
----> 2 values = loadtxt("values.txt", float)
      3 mean = sum(values)/len(values)
      4 print(mean)

~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows)
    960             fname = os_fspath(fname)
    961         if _is_string_like(fname):
--> 962             fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
    963             fencoding = getattr(fh, 'encoding', 'latin1')
    964             fh = iter(fh)

~\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(path, mode, destpath, encoding, newline)
    264 
    265     ds = DataSource(destpath)
--> 266     return ds.open(path, mode, encoding=encoding, newline=newline)
    267 
    268 

~\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(self, path, mode, encoding, newline)
    622                                       encoding=encoding, newline=newline)
    623         else:
--> 624             raise IOError("%s not found." % path)
    625 
    626 

OSError: values.txt not found.

I have the values.txt file saved in my documents folder. Do I need to save it in some specific folder so Python can find it?

You can either use the absolute path , or use loadtxt("values.txt", float) but then your file should be in the same folder with your script/jupyter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM