烧瓶:保存上传文件时IOError [英] Flask: IOError when saving uploaded files

查看:187
本文介绍了烧瓶:保存上传文件时IOError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Flask,并试图通过这里记录的上传文件模式: http: //flask.pocoo.org/docs/patterns/fileuploads/ 。我正在使用Windows 7上的Firefox 12,并且正在我的本地计算机上以调试模式运行我的应用程序。



我正在复制范例,除了值UPLOAD_FOLDER变量,我已经定义为 UPLOAD_FOLDER ='/ uploads',并创建了一个名为uploads的目录,它存在于应用程序根目录中在上传文件时,我收到错误: IOError:[Errno 2]没有这样的文件或目录:'/有趣的是,如果我指定一个直接指向上传的uploads文件夹的原始字符串,在我的机器上,如 UPLOAD_FOLDER = r'C:\Python27\projects\Flask\myproject\uploads',一切正常。



我没有以正确的方式指定目录吗?上传目录应放在其他地方吗?

解决方案

前面的斜线表示文件系统层次结构的根。虽然这可能不是Windows上的工作方式,但Python可以通过这种方式来理解它,因为它的路径处理函数是跨平台的。



表单'uploads /'和'./uploads/'是等价的,而且是相对的。请注意,相对路径是相对于当前目录而言的,您不必控制它,因此您可能需要为UPLOAD_FOLDER指定一个绝对路径。


I am learning Flask and am attempting to work through the uploading files pattern documented here: http://flask.pocoo.org/docs/patterns/fileuploads/. I am working in Firefox 12 on Windows 7, and am running my app in debug mode on my local machine.

I am copying the example verbatim, except for the value of the UPLOAD_FOLDER variable, which I have defined as UPLOAD_FOLDER = '/uploads' and have created a directory called "uploads" which is present in the application root (along with the static and template directories).

Upon uploading the file, I am receiving the error: IOError: [Errno 2] No such file or directory: '/uploads\\u.png'

Interestingly, if I specify a raw string for the uploads folder which points directly to the uploads directly on my machine like UPLOAD_FOLDER = r'C:\Python27\projects\Flask\myproject\uploads', everything works just fine.

Am I not specifying the directory in the right way? Should the uploads directory be placed somewhere else?

解决方案

The slash at the beginning of '/uploads' makes the path specification absolute: the leading slash represents the root of the filesystem hierarchy. While that might not be exactly how things work on Windows, it makes sense for Python to understand it this way as its path-handling functions are cross-platform.

The forms 'uploads/' and './uploads/' are equivalent and they are relative.

Note that relative paths are relative to the current directory, which you don't necessarily control, so you might want to specify an absolute path for UPLOAD_FOLDER.

这篇关于烧瓶:保存上传文件时IOError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆