NumPy genfromtxt OSError: file not found - python

I'm learning the NumPy library and when I try to read something from the file I get this error:
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\Reading_from_file.py", line 3, in <module>
example = genfromtxt("example.txt", delimiter=',')
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\npyio.py", line 1793, in genfromtxt
fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 193, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 533, in open
raise IOError("%s not found." % path)
OSError: example.txt not found.
Here's the code:
from numpy import genfromtxt
example = genfromtxt("example.txt", delimiter=',')
Reading_from_file.py and example.txt are in the same folder
I read the documentation and I was trying to find something here but found nothing (maybe I missed something)
If there is already a thread on this topic, please link to it

You probably aren't running the script from the same folder that example.txt is in. example.txt doesn't need to be in the same directory as the script itself, it needs to be in the same directory as you are when you're running the script.

This is not exactly your scenario, but I got the same error while passing a string to genfromtxt and I was confused.
In fact, genfromtxt expects a file path by default. If you want to pass a string you need to simulate a file by wrapping the string in a StringIO object:
from io import StringIO
from numpy import genfromtxt
example = genfromtxt(StringIO("1, 3"), delimiter=',')
print(example)
Returns:
[1. 3.]

Related

cannot read simple txt file in python

I am a beginner. I have a simple txt file which I need to read (using numpy). I have the program in the same directory as the .txt file.
I have checked the cwd and it's the right one. Also, I've written a text file in order to see if python wants to open that one - that file opens just fine.
import os
import numpy as np
np.loadtxt("test2.txt")
The code above gives me the error.
The code below works just fine.
import os
import numpy as np
x = np.array([1, 2, 3])
np.savetxt("test.txt", x)
y = np.loadtxt("test.txt")
print(y)
The error I get is:
Traceback (most recent call last):
File "D:\detest\admi.py", line 5, in <module>
np.loadtxt("test2.txt")
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\npyio.py", line 962, in loadtxt
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 266, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 624, in open
raise IOError("%s not found." % path)
OSError: test2.txt not found.
Can you use the Python read file instead?
path = '' # location of your file
openfile = open(path, 'r') # open file
openfile.read() # return all content of file
openfile.close() # close file

How do I to turn my .tar.gz file into a file-like object for shutil.copyfileobj?

My goal is to extract a file out of a .tar.gz file without also extracting out the sub directories that precede the desired file. I am trying to module my method off this question. I already asked a question of my own but it seemed like the answer I thought would work didn't work fully.
In short, shutil.copyfileobj isn't copying the contents of my file.
My code is now:
import os
import shutil
import tarfile
import gzip
with tarfile.open('RTLog_20150425T152948.gz', 'r:*') as tar:
for member in tar.getmembers():
filename = os.path.basename(member.name)
if not filename:
continue
source = tar.fileobj
target = open('out', "wb")
shutil.copyfileobj(source, target)
Upon running this code the file out was successfully created however, the file was empty. I know that this file I wanted to extract does, in fact, have lots of information (approximately 450 kb). A print(member.size) returns 1564197.
My attempts to solve this were unsuccessful. A print(type(tar.fileobj)) told me that tar.fileobj is a <gzip _io.BufferedReader name='RTLog_20150425T152948.gz' 0x3669710>.
Therefore I tried changing source to: source = gzip.open(tar.fileobj) but this raised the following error:
Traceback (most recent call last):
File "C:\Users\dzhao\Desktop\123456\444444\blah.py", line 15, in <module>
shutil.copyfileobj(source, target)
File "C:\Python34\lib\shutil.py", line 67, in copyfileobj
buf = fsrc.read(length)
File "C:\Python34\lib\gzip.py", line 365, in read
if not self._read(readsize):
File "C:\Python34\lib\gzip.py", line 433, in _read
if not self._read_gzip_header():
File "C:\Python34\lib\gzip.py", line 297, in _read_gzip_header
raise OSError('Not a gzipped file')
OSError: Not a gzipped file
Why isn't shutil.copyfileobj actually copying the contents of the file in the .tar.gz?
fileobj isn't a documented property of TarFile. It's probably an internal object used to represent the whole tar file, not something specific to the current file.
Use TarFile.extractfile() to get a file-like object for a specific member:
…
source = tar.extractfile(member)
target = open("out", "wb")
shutil.copyfile(source, target)

IO error in savetxt while using numpy

Im trying to read a dataset and collect meta features from it.
I get the following error after executing the python file.
Traceback (most recent call last):
File "runmeta.py", line 79, in <module>
np.savetxt('datasets/'+str(i)+'/metafeatures',meta[i],delimiter=',')
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 940, in savetxt
fh = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'datasets/2/metafeatures'
the error you're getting is simply telling you it didn't find the file. i would suggest looking into absolute and relative file paths.
advice in error handling:
the error is triggered on this line
fh = open(fname, 'w')
so as you debug your program, look at the line python shows you. maybe change the variable fname. that is where i would start.
currently
fname = 'datasets/2/metafeatures'

Failed to interpret file %s as a pickle when loading an npy array

Hi I am a new user to python and want to import a saved npy array. When attempting to load the npy array, I get the following error message. Thanks in advance!
import numpy as np
A = np.load('C:/Final Runs/lineTank.npy')
I receive these errors:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 384, in load
"Failed to interpret file %s as a pickle" % repr(file))
IOError: Failed to interpret file 'C:/Final Runs/lineTank.npy' as a pickle
Thanks for the help guys. I realized #Joe Kington was right. The file was creating using the traditional python write to a file as opposed to numpy. This is what I had used (which didn't work):
f = open(Filename, "w")
try:
f.write(a)
finally:
f.close()
as opposed to using numpy's save, which works:
import numpy as np
np.save(Filename, a)
a = np.load(Filename)
You might need to load the file into a string first, then numpy.load() the string.
Something like :
with f as file.open(filename):
foo = f.readlines()
bar = numpy.load(foo)

Error in reading hdf file using h5py package for python

I want to extract data from hdf files that I downloaded from MODIS website. A sample file is provided in the link. I am reading the hdf file by using the following lines of code:
>>> import h5py
>>> f = h5py.File( 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf', 'r' )
The error I am getting:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
f = h5py.File( 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf', 'r' )
File "C:\Python27\lib\site-packages\h5py\_hl\files.py", line 165, in __init__
fid = make_fid(name, mode, userblock_size, fapl)
File "C:\Python27\lib\site-packages\h5py\_hl\files.py", line 57, in make_fid
fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
File "h5f.pyx", line 70, in h5py.h5f.open (h5py\h5f.c:1640)
IOError: unable to open file (File accessability: Unable to open file)
I have tried several other hdf files from different sources but I am getting the same error. What seems to be the fault here?
I think there could be two possible problems:
1) As the file extension is "hdf", maybe this is a HDF4 file. HDF5 files normally have ".hdf5" or ".h5·" extension. I am not sure if h5py is able to read HDF4 files.
2) Perhaps you have to change permissions to the file itself. If you are in a linux machine try: chmod +r file.hdf
You can try to open your file with HDFView. This software is available in several platforms. You can check the properties of the files very easily with it.
This sounds like a file permission error, or even file existence. Maybe add some checks such as
import os
hdf_file = 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf'
if not os.path.isfile(hdf_file):
print 'file %s not found' % hdf_file
if not os.access(hdf_file, os.R_OK):
print 'file %s not readable' % hdf_file
f = h5py.File(hdf_file, 'r')
I had the same issue, and later identified that my file had only "read-only", which for some reason stopped the h5py to read it. After modifying the permission to "write", I was able to read it. Not sure why it was set up like this.

Categories