[英]IOError: [Errno 2] No such file or directory, but file does exist
I keep getting an IO Error stating my directory doesn't exist. 我不断收到IO错误,指出我的目录不存在。 What am I doing wrong?
我究竟做错了什么?
I have this in a separate file named pirate.py : 我在一个名为pirate.py的单独文件中拥有此文件:
with open("/images/image.jpg", "rb") as fin:
image_data = fin.read()
with open("pirate.py","wb") as fout:
fout.write("image_data="+repr(image_data))
And then the code in my main file 然后在我的主文件中的代码
from pirate import image_data
# Content-type declaration
print('Content-type: text/html\n')
def main():
print('<!doctype html><head><meta charset="utf-8">')
print('<style>html {background:url (data:image/gif;base64,' + pirate.image_data + ')
I'm trying to encode an image in base64 and then use it as a background in a .cgi, I'm sure the rest of my code is working, what am I doing wrong here? 我正在尝试在base64中编码图像,然后将其用作.cgi中的背景,我确定我的其余代码都可以正常工作,我在这里做错了什么?
try to use absolute file paths. 尝试使用绝对文件路径。 or use root prefix ROOT_PATH = os.path.dirname(os.path.realpath( file ))
或使用根前缀ROOT_PATH = os.path.dirname(os.path.realpath( file ))
repr is not what you want, you are saying the image data is base64 ... so make it base 64 repr不是您想要的,您说的是图像数据是base64 ...因此使其成为base64
with open("pirate.py","wb") as fout:
fout.write("image_data="+base64.b64encode(image_data))
then in addition you say 然后你还说
from pirate import image_data
then you reference it as 然后您将其引用为
pirate.image_data
when you should just reference it as 当你应该引用它为
image_data
theres probably some other probelems since the image image path you show is not the image path from your comment 还有其他一些问题,因为您显示的图像图像路径不是您评论中的图像路径
Your code says 您的代码说
open("/images/image.jpg", "rb")
I suspect the "/" at the beginning is the problem. 我怀疑开头的“ /”是问题所在。 Or is your images folder in your root?
还是您的图片文件夹在您的根目录中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.