bottle.py使用file.save()引发ValueError('已关闭文件的I / O操作'))>

问题描述 投票:1回答:2

对于我当前的项目,我正在使用python瓶。目前,我正在尝试保存用户以表格形式上载的文件,但这会引起上面显示的错误。我以最好的方式跟踪文档,但是无论我尝试了什么,它都会出现此错误。

这里是功能:

def uploadFile(file, isPublic, userId, cu, dirId=-1):
    """Takes a bottle FileUpload instance as an argument and adds it to storage/media as well as adds its 
    info to the database. cu should be the db cursor. isPublic should be a bool. userId should be the 
    uploaders id. dirId is the directory ID and defaults to -1."""
    fakeName = file.filename
    extension = os.path.splitext(file.filename)[1]
    cu.execute("SELECT * FROM files WHERE fileId=(SELECT MAX(fileId) FROM files)")
    newId = cu.fetchone()
    if newId==None:
        newId = 0
    else:
        newId = newId[1]
    if debugMode:
        print(f"newId {newId}") 
    fileName = f"userfile-{newId}-{userId}.{extension}"
    file.save(vdata["m_folder"] + "/" + fileName)
    cu.execute("INSERT INTO files VALUES (?, ?, ?, ?, ?, ?)",
    (userId, newId, dirId, fakeName, fileName, isPublic))
    cu.connection.commit()

有人知道这可能是什么问题吗?

对于我当前的项目,我正在使用python瓶。目前,我正在尝试保存用户以表格形式上载的文件,但这会引起上面显示的错误。我以最佳方式关注了文档...

python python-3.x bottle
2个回答
1
投票

似乎您尚未打开要写入的文件。您可以这样做:


0
投票

使用Bottle框架上传文件的示例

© www.soinside.com 2019 - 2024. All rights reserved.