简体   繁体   English

IOError:[Errno 2]没有此类文件或目录,但文件确实存在

[英]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.

相关问题 IOError:[Errno 2]没有这样的文件或目录 - IOError: [Errno 2] No such file or directory IOError:[Errno 2]没有这样的文件或目录? - IOError: [Errno 2] No such file or directory? IOError: [Errno 2] 没有这样的文件或目录:(Python:即使文件确实存在,我也遇到了这个错误) - IOError: [Errno 2] No such file or directory: (Python: I am having this error even though the file does exist) BioPython:IOError:[Errno 2]没有这样的文件或目录 - BioPython: IOError: [Errno 2] No such file or directory AppEngine-IOError:[错误2]没有此类文件或目录: - AppEngine - IOError: [Errno 2] No such file or directory: IOError:Errno 2没有这样的文件或目录python - IOError: Errno 2 No such file or directory python PyInstaller:IO错误:[Errno 2]没有这样的文件或目录: - PyInstaller: IOError: [Errno 2] No such file or directory: Python IOError:[Errno 2]没有这样的文件或目录 - Python IOError: [Errno 2] No such file or directory IOError:[Errno 2]没有这样的文件或目录:但是文件在那里… - IOError: [Errno 2] No such file or directory: but the files are there… IOError:[Errno 2]偶尔没有这样的文件或目录 - IOError: [Errno 2] No such file or directory occasionally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM