簡體   English   中英

IOError:[Errno 2]沒有此類文件或目錄,但文件確實存在

[英]IOError: [Errno 2] No such file or directory, but file does exist

我不斷收到IO錯誤,指出我的目錄不存在。 我究竟做錯了什么?

我在一個名為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))

然后在我的主文件中的代碼

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

我正在嘗試在base64中編碼圖像,然后將其用作.cgi中的背景,我確定我的其余代碼都可以正常工作,我在這里做錯了什么?

嘗試使用絕對文件路徑。 或使用根前綴ROOT_PATH = os.path.dirname(os.path.realpath( file ))

repr不是您想要的,您說的是圖像數據是base64 ...因此使其成為base64

with open("pirate.py","wb") as fout:
  fout.write("image_data="+base64.b64encode(image_data))

然后你還說

from pirate import image_data

然后您將其引用為

pirate.image_data

當你應該引用它為

image_data     

還有其他一些問題,因為您顯示的圖像圖像路徑不是您評論中的圖像路徑

您的代碼說

open("/images/image.jpg", "rb")

我懷疑開頭的“ /”是問題所在。 還是您的圖片文件夾在您的根目錄中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM