繁体   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