简体   繁体   中英

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. If I enter the exact same file url in a browser the photo appears.

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:

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.

If you are trying to open a file over internet you should do something like this (assuming that you are using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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