Python TypeError:文件必须有';阅读';和';readline';属性

Python TypeError:文件必须有';阅读';和';readline';属性,python,Python,第一种方法: 我试图使下面的代码从早上开始工作。我在stackoverflow和关于python的谷歌教程中读到了很多答案,我已经取得了0的进步。你能帮我解决这个错误吗 Using TensorFlow backend. Traceback (most recent call last): File "source_code_modified.py", line 65, in <module> dict1 = pickle.load(f1,encoding='bytes'

第一种方法: 我试图使下面的代码从早上开始工作。我在stackoverflow和关于python的谷歌教程中读到了很多答案,我已经取得了0的进步。你能帮我解决这个错误吗

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
第二种方法:

好的,我这样做了,错误是:

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'
使用TensorFlow后端。
回溯(最近一次呼叫最后一次):
文件“source_code_modified.py”,第74行,在
打开的列车数据()为f1时:
文件“source_code_modified.py”,第47行,在open_train_数据中
返回打开状态('/home/just_learning/Desktop/CNN/datasets/training_data/images','rb')
IsADirectoryError:[Errno 21]是一个目录:'/home/just_learning/Desktop/CNN/datasets/training_data/images'
代码在这些点上爆炸:

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
def open_train_data():
返回open('/home/just_learning/Desktop/CNN/datasets/training_data/images','rb')使用类似于:

def open_test_data():
    return open('path/to/filename', 'rb')

with open_test_data() as f:
    dict1 = pickle.load(f) 
没有理由尝试为这样的内容定义自己的上下文管理器

编辑:您最初的第一次尝试是
编码class='bytes'
。根据数据的来源,您可能需要添加这些数据。

使用类似于:

def open_test_data():
    return open('path/to/filename', 'rb')

with open_test_data() as f:
    dict1 = pickle.load(f) 
没有理由尝试为这样的内容定义自己的上下文管理器


编辑:您最初的第一次尝试是
编码class='bytes'
。根据数据的来源,您可能需要添加它。

您是否尝试过picke.loadS忽略大的S?我这样做是为了让您看得更清楚。您对类定义的工作方式有误解。
os.open
调用仅在类定义上执行,而不是在实例化类时执行。
pickle.load
的第一个参数应该是“类似文件的对象”;这意味着在这种情况下,它需要有
read
readline
方法。您的第二种方法会给出错误消息:
是一个目录
。这是一个提示…提示:
pickle.load
用于加载由
pickle.dump
创建的文件。那么我应该如何处理pickle.dump?您是否尝试过picke.loadS忽略我这样做,以便您可以更好地看到。您对类定义的工作方式有误解。
os.open
调用仅在类定义上执行,而不是在实例化类时执行。
pickle.load
的第一个参数应该是“类似文件的对象”;这意味着在这种情况下,它需要有
read
readline
方法。您的第二种方法会给出错误消息:
是一个目录
。这是一个提示…提示:
pickle.load
用于加载由
pickle.dump
创建的文件。因此,我应该如何处理pickle.dump?因为我正在搜索,但我无法确定如何使其工作。有没有办法加载存储在我的电脑(“png”)中的图像数据集,以便可以通过以下方式读取:trainX、trainY、testX、testY??因为我发现的大部分内容都是这样的:(x_train,y_train),(x_test,y_test)=mnist.load_data()或(trainX,trainY),(testX,testY)=cifar10.load_data()。另一个问题:我可以将包含图像的文件夹转换为pickle(.P)文件吗?对于这个问题,你可以发布一个新问题。因为我正在搜索,但我不知道如何使它工作。有没有办法加载存储在我的电脑(“png”)中的图像数据集,以便可以通过以下方式读取:trainX、trainY、testX、testY??因为我发现的大部分内容都是这样的:(x_train,y_train),(x_test,y_test)=mnist.load_data()或(trainX,trainY),(testX,testY)=cifar10.load_data()。另一个问题:我可以将包含图像的文件夹转换为pickle(.P)文件吗?为此,您可以发布一个新问题。
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__
def open_test_data():
    return open('path/to/filename', 'rb')

with open_test_data() as f:
    dict1 = pickle.load(f)