简体   繁体   English

python open说文件不存在

[英]Python open says file doesn't exist when it does

I am trying to work out why my Python open call says a file doesn't exist when it does. 我试图弄清楚为什么我的Python open调用会说一个文件不存在。 If I enter the exact same file url in a browser the photo appears. 如果在浏览器中输入完全相同的文件URL,则会出现照片。

The error message I get is: 我收到的错误消息是:

No such file or directory: 'https://yhistory.s3.amazonaws.com/media/userphotos/1_1471378042183_cdv_photo_033.jpg'

The Python code is: Python代码是:

full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
fd_img = open(full_path_filename, 'r')

I was wondering if this problem is something to do with the file being in an AWS S3 bucket but I am able to connect to the bucket and list its contents. 我想知道这个问题是否与文件在AWS S3存储桶中有关,但是我能够连接到存储桶并列出其内容。

If you are trying to open a file over internet you should do something like this (assuming that you are using python 3): 如果您尝试通过Internet打开文件,则应执行以下操作(假设您使用的是python 3):

import urllib.request
full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name

file = urllib.request.urlopen(full_path_filename)

This will download the actual file. 这将下载实际文件。 You can use file as an another file like object. 您可以将file用作对象之类的另一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM