Jerod Lake
Jerod Lake

Reputation: 1

Python Numpy Builtins Issue

I am attempting to load .bin files into Python as a uint8 then do stuff to them then load in the next file:

for filepath in bin_files:
    data = np.fromfile(filepath,dtype=np.uint8)

bin_files is my list of files that all look fine. np is the numpy library that I load in upon running the script. The error I get is in np.fromfile. When I run the script the error generated is:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File c:\users\jlake\onedrive - cidra\desktop\python\sonaralgo\decryptbinfiles.py:24
     21             bin_files.append(os.path.join(root, file))
     23 for filepath in bin_files:
---> 24     data = np.fromfile(filepath,dtype=np.uint8)
     25     S = len(data)
     26     match S:

KeyError: '__builtins__'

The weird part is after the error if I highlight the np.fromfile line and hit F9 the line runs fine and I get my data loaded.

I tried all different things over several days. Currently I just bypass the error by running things using F9. I am using Spyder as an IDE and F9 runs the line or lines of coded highlighted at the time.

The files were generated using our legacy product (not Python). The weird part is if I execute that line using F9 (in Spyder) it runs fine. I only get that error when I hit "run file", or "debug file" in Spyder.


This doesn't actually fix the problem but is a work around: I replaced

data = np.fromfile(filepath,dtype=np.uint8)

with:

with open(filepath, 'rb') as file:
    data = array.array('B',file.read())
data = np.array(data)

Now I can run the script without without error and without using F9.

Upvotes: 0

Views: 70

Answers (0)

Related Questions