question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.fromfile OSError: first argument must be an open file (Python 3)

See original GitHub issue

Using Python 3 and Numpy 1.9.2 there is an issue with numpy.fromfile when called on an existing file object. This code works fine in Python 2.7.

$ fh
<_io.FileIO name=8 mode='rb+'>
$ np.fromfile(fh, dtype=dtype, count=length)
OSError: first argument must be an open file

I’ve checked fh.closed is False, so the file is still open. The error is thrown by numpy.multiarraymodule.c#2119.

Someone mentioned these issues on Stack Overflow, but no clear solutions are present.

Any suggestions?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
rpratt20commented, Oct 8, 2015

Someone pointed out to me that fromfile is designed to read straight from a file and not buffered. Using this: trace.data = np.frombuffer(fpin.read(record_dtype.itemsize * npts), record_dtype) gives me correct input so there probably is no issue. Thanks.

Randy ----- Original Message ----- From: W. Trevor King To: numpy/numpy Cc: rpratt20 Sent: Wednesday, October 07, 2015 2:35 PM Subject: Re: [numpy] numpy.fromfile OSError: first argument must be an open file (Python 3) (#6246)

Is this closable now that #6330 has landed, or should it stay open until @rpratt20’s Python 2 issue has been addressed?

— Reply to this email directly or view it on GitHub.

0reactions
cjermaincommented, Oct 8, 2015

Thanks @charris!

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Obtaining file position failed" while using ndarray.tofile() and ...
So my question is, how I should use the np.fromfile() and the ndarray.tofile() correctly in this case. python · python-3.x ...
Read more >
numpy.fromfile — NumPy v1.24 Manual
Construct an array from data in a text or binary file. A highly efficient way of reading binary data with a known data-type,...
Read more >
Reading and Writing Files in Python - DataCamp
Let's understand the first argument, i.e., file . file. file is a mandatory argument that you have to provide to the open function...
Read more >
os.path — Common pathname manipulations — Python 3.11 ...
To read or write files see open(), and for accessing the filesyst... ... On Unix and Windows, return the argument with an initial...
Read more >
pyfits 3.4 - PyPI
pyfits.open no longer catches OSError exceptions on missing or unreadable files– instead it raises the standard Python exceptions in such cases.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found