Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表中的文件路径不会加载python_Python - Fatal编程技术网

列表中的文件路径不会加载python

列表中的文件路径不会加载python,python,Python,如果我硬编码路径,我可以加载图像,但是当我试图从列表中提取字符串时,我不断收到错误消息。不知道我做错了什么 #for i in range(0,len(training_YFT)): #print(training_YFT[i]) #image = Image.open("/media/rafael/Data1/train/YFT/img_00004.jpg") image = Image.open(training_YFT[0]) #image = Image.open(trainin

如果我硬编码路径,我可以加载图像,但是当我试图从列表中提取字符串时,我不断收到错误消息。不知道我做错了什么

#for i in range(0,len(training_YFT)):
    #print(training_YFT[i])
#image = Image.open("/media/rafael/Data1/train/YFT/img_00004.jpg")
image = Image.open(training_YFT[0])
#image = Image.open(training_YFT[i]).convert("L")
arr = np.asarray(image)
plt.imshow(arr,  cmap='gray')
plt.pause(0.01)
plt.show()
我将收到的错误消息粘贴到下面。n

Traceback (most recent call last):
  File "/home/rafael/anaconda3/lib/python3.5/site-packages/PIL/Image.py", line 2283, in open
    fp.seek(0)
AttributeError: 'str' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "fishy.py", line 95, in <module>
    image = Image.open(training_YFT[0])
  File "/home/rafael/anaconda3/lib/python3.5/site-packages/PIL/Image.py", line 2285, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'
第一个错误是,您试图调用的对象是file对象,但变量实际上是string对象

str对象没有名为seek的函数

接下来是列表问题

尝试从列表中打印出来,确保它不是str

[‘路径1’、‘路径2’]

如果它确实是一个列表,它应该返回给您path1

如果它是一个字符串,它将返回p,这不是您想要的

接下来,看起来类映像需要一个文件对象,您正在向它传递一个字符串。虽然很奇怪,你之前给了它一根绳子,它似乎起作用了。我的建议是试试Image.openvariable

AttributeError:“str”对象没有属性“read”

我从来没有玩过PIL包,但是,你能做的最好的事情就是在函数前后打印变量,确保它们是你想要传递的变量

print(training_YFT)
if not type(training_YFT) == list: print('training_YFT IS NOT A LIST, you\'re giving the function '+training_YET[0]+'!')
image = Image.open(training_YFT[0])
print(image)
arr = np.asarray(image)
plt.imshow(arr,  cmap='gray')
plt.pause(0.01)
plt.show()
另一个建议是确保文件路径的存在。您可以通过导入os.path isfile来完成此操作。例如,从os.path导入isfile


因为没有所有的源代码,没有足够的信息来准确判断,所以无法准确地告诉您代码到底出了什么问题。但我希望这会有所帮助,并给你一些想法,让你在事情不顺利的时候尝试。

非常奇怪。您能执行dirtraining_YFT[0]并向我们显示结果吗?谢谢,我发现问题列表的第一个元素为空。我使用空字符串对其进行了测试,没有预期的文件或目录消息。奇怪的是,我相信你,因为消息中说已经传递了str对象,但是应该尝试打开文件,而不是在它上查找。如果你看一下PIL文档,它们似乎表明传递字符串或文件是可以的。
print(training_YFT)
if not type(training_YFT) == list: print('training_YFT IS NOT A LIST, you\'re giving the function '+training_YET[0]+'!')
image = Image.open(training_YFT[0])
print(image)
arr = np.asarray(image)
plt.imshow(arr,  cmap='gray')
plt.pause(0.01)
plt.show()