TypeError: file must have 'read' and 'readline' attributes

jus*_*ing 6 python

1st approach: I am trying to make the below code work since morning. I have read many answers here in stackoverflow and tutorials on google about python, I have done 0 progress. Can you help me with this error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "source_code_modified.py", line 65, in <module>
    dict1 = pickle.load(f1,encoding='bytes')
TypeError: file must have 'read' and 'readline' attributes
Run Code Online (Sandbox Code Playgroud)

The code that explodes is this part:

class load_train_data:
    os.open("/home/just_learning/Desktop/CNN/datasets/training_data/images", os.O_RDONLY)
    def __enter__(self):
        return self
    def __exit__(self, type, value, traceback):
        pass


class load_test_data:
    os.open("/home/just_learning/Desktop/CNN/datasets/test_data/images",os.O_RDONLY) 
    def __enter__(self):
        return self
    def __exit__(self, type, value, traceback):
        pass


with load_train_data() as f1:
   dict1 = pickle.load(f1,encoding='bytes')
Run Code Online (Sandbox Code Playgroud)

2nd approach:

Ok, I did that, the error is:

Using TensorFlow backend.
Traceback (most recent call last):
  File "source_code_modified.py", line 74, in <module>
    with open_train_data() as f1:
  File "source_code_modified.py", line 47, in open_train_data
    return open('/home/just_learning/Desktop/CNN/datasets/training_data/images','rb') 
IsADirectoryError: [Errno 21] Is a directory: '/home/just_learning/Desktop/CNN/datasets/training_data/images'
Run Code Online (Sandbox Code Playgroud)

And the code explodes on these points:

def open_train_data():
    return open('/home/just_learning/Desktop/CNN/datasets/training_data/images','rb') <--- explodes here

def open_test_data():
    return open('/home/just_learning/Desktop/CNN/datasets/test_data/images','rb')

with open_train_data() as f1:
   dict1 = pickle.load(f1)   <--- explodes here
Run Code Online (Sandbox Code Playgroud)

3rd approach: I found this: "IsADirectoryError: [Errno 21] Is a directory: " It is a file

and I changed the code to this:

def open_train_data():
    return os.listdir('/home/just_learning/Desktop/CNN/datasets/training_data/images') 

def open_test_data():
    return os.listdir('/home/just_learning/Desktop/CNN/datasets/test_data/images')

with open_train_data() as f1:           <-------------- explodes here
   dict1 = pickle.load(f1) #,encoding='bytes')
Run Code Online (Sandbox Code Playgroud)

and the error is this:

Using TensorFlow backend.
Traceback (most recent call last):
  File "source_code_modified.py", line 74, in <module>
    with open_train_data() as f1:
AttributeError: __enter__
Run Code Online (Sandbox Code Playgroud)

and this error I solved it by searching in github/google and was the result depicted on the first approach, that I have posted above...

que*_*o42 11

只是向其他受影响的人分享一些愚蠢的错误(由于愚蠢)^^:

我遇到错误是TypeError: file must have 'read' and 'readline' attributes因为我使用纯文本 file_path 而不是打开的f文件对象作为 pickle 函数的参数。

正确的:

file_path = 'path/to/filename'

with open(file_path , 'rb') as f:
    dict1 = pickle.load(f)
Run Code Online (Sandbox Code Playgroud)

错误的:

file_path = 'path/to/filename'

with open(file_path , 'rb') as f:
    dict1 = pickle.load(file_path)
Run Code Online (Sandbox Code Playgroud)