簡體   English   中英

如何在Google App Engine中的python中打開文件?

[英]how to open a file in python in google app engine?

 new_file = open('../pages/pdf/test.PDF', 'rb').read()

一直給我找不到文件/目錄

我也在app.yaml中嘗試了這個:

 - url: /test.((pdf)|(PDF))
   static_files: pages/pdf/test.PDF
   upload: pages/pdf/test.PDF

檔案結構

 -app.yaml
 pages
   -index.html
   -page1.html
   pdf
     -test.PDF
 entities
   -booking.py (file that im writing the above code)

如何在Google App Engine中正確打開文件?

這是app.yaml處理程序

 - url: /sitemap.xml
   script: sitemap.app

 - url: /test.((pdf)|(PDF))
   static_files: pages/pdf/test.PDF
   upload: pages/pdf/test.PDF
   application_readable: true

 - url: /images
   static_dir: pages/images

 - url: /fonts
   static_dir: pages/fonts

 - url: /css
   static_dir: pages/css
   expiration: 1s

嘗試使用EmailMessage如下附加上述文件:

 new_file = open(os.path.dirname(__file__) +'/../pages/pdf/test.PDF').read()

 message = mail.EmailMessage( sender=EMAIL_SENDER, 
 subject=subject,body=theBody,to=['myemail@gmail.com'],attachments= 
 [('test',new_file)])

 message.send()

在調試中說:無效的附件類型

默認情況下,應用程序代碼無法訪問應用程序靜態內容中包含的文件(即與static_filesstatic_dir處理程序配置模式匹配)。 如果要進行此類訪問,則需要為各個app.yaml處理程序配置設置application_readable: true選項。 請參閱GAE:file_get_contents()和靜態文件

一旦執行此操作,就可以使用相對於應用程序/服務根目錄(即app.yaml文件所在的位置)的路徑訪問該文件,在您的情況下,它將是pages/pdf/test.PDF

暫無
暫無

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

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