python open表示文件不存在 [英] Python open says file doesn't exist when it does

查看:126
本文介绍了python open表示文件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚为什么我的Python open调用说一个文件不存在.如果我在浏览器中输入完全相同的文件URL,则会显示照片.

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.

我收到的错误消息是:

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

Python代码是:

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')

我想知道这个问题是否与AWS S3存储桶中的文件有关,但是我能够连接到存储桶并列出其内容.

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.

推荐答案

如果您尝试通过Internet打开文件,则应执行以下操作(假设您使用的是python 3):

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)

这将下载实际文件.您可以将 file 用作另一个文件,例如object.

This will download the actual file. You can use file as an another file like object.

这篇关于python open表示文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆