Search code examples
pythonnumpygenfromtxt

numpy.genfromtxt path value


I would like to load a txt file with genformtxt(). the txt file is already in c:.

stock=np.genfromtxt('c:\09012017.txt',delimiter=' ',dtype=str,skip_header=1)

C:\Anaconda3\lib\site-packages\numpy\lib\npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows)
   1549                 fhd = iter(np.lib._datasource.open(fname, 'rbU'))
   1550             else:
-> 1551                 fhd = iter(np.lib._datasource.open(fname, 'rb'))
   1552             own_fhd = True
   1553         else:

C:\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(path, mode, destpath)
    149 
    150     ds = DataSource(destpath)
--> 151     return ds.open(path, mode)
    152 
    153 

C:\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(self, path, mode)
    492 
    493         # NOTE: _findfile will fail on a new file opened for writing.
--> 494         found = self._findfile(path)
    495         if found:
    496             _fname, ext = self._splitzipext(found)

C:\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in _findfile(self, path)
    335 
    336         for name in filelist:
--> 337             if self.exists(name):
    338                 if self._isurl(name):
    339                     name = self._cache(name)

C:\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in exists(self, path)
    440 
    441         # Test local path
--> 442         if os.path.exists(path):
    443             return True
    444 

C:\Anaconda3\lib\genericpath.py in exists(path)
     17     """Test whether a path exists.  Returns False for broken symbolic links"""
     18     try:
---> 19         os.stat(path)
     20     except OSError:
     21         return False

ValueError: stat: embedded null character in path

it looks like sth wrong in path . I am 100% sure the txt is under disc c: path. pls give some help. Thanks


Solution

  • You either have to use a forward slash, or a double backslash

    stock=np.genfromtxt('c:/09012017.txt',delimiter=' ',dtype=str,skip_header=1)
    

    or

    stock=np.genfromtxt('c:\\09012017.txt',delimiter=' ',dtype=str,skip_header=1)
    

    If you use just one backslash it will be seen as an escape command, which is not the thing you want to do there.

    If you just call the filename and not the absolute path, the file has to be located in the working directory of the python process; this is the working directory of the shell, which started you python process.