代码之家  ›  专栏  ›  技术社区  ›  zennith

文件上传问题-Python WIndows Cherylpy

  •  1
  • zennith  · 技术社区  · 10 年前

    我是蟒蛇和樱桃的新手。我正在尝试使用以下代码上载文件:

    @cherrypy.tools.noBodyProcess()
    def POST(self,theFile=None):
        lcHDRS = {}
        for key, val in cherrypy.request.headers.iteritems():
            lcHDRS[key.lower()] = val
       formFields = myFieldStorage(fp=cherrypy.request.rfile,
                                    headers=lcHDRS,
                                    environ={'REQUEST_METHOD':'POST'},
                                    keep_blank_values=True)
    
        dt = datetime.now()
        date = dt.strftime('%Y-%m-%d')
        dt = dt.strftime('%Y%m%d%H%M%S')
        theFile = formFields['theFile']
        theFile.filename = str(dt) + "file"
        shutil.copy2(theFile.file.name,os.path.join(absolutePath , theFile.filename))
        ...
        ...
    

    我检查了路径os.path.join(absolutePath,file.filename),它是正确的。 问题是代码在Linuxubuntu上运行良好,但在windows上运行不好。 调用的错误为: 已编辑

    shutil.copy2(theFile.file.name,settings.UPLOAD_FILE_PATH + theFile.filename)
    File "C:\Anaconda\lib\shutil.py", line 130, in copy2
    copyfile(src, dst)
    File "C:\Anaconda\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
    IOError: [Errno 13] Permission denied: 'c:\\users\\username\\appdata\\local\\temp\\tmpjy3gys'
    

    我哪里出错了? 如果你需要任何其他信息,请告诉我。

    2 回复  |  直到 10 年前
        1
  •  2
  •   saaj    10 年前

    该问题可能与某些临时文件安全性有关,该安全性禁止通过文件名重新打开。尝试替换 shutil.copy2 呼叫:

    with open('/path/that/you/have/permission/to', 'wb') as f:
      shutil.copyfileobj(theFile.file, f)
    
        2
  •  0
  •   Cui Heng    10 年前

    我想windows对启动程序有UAC限制,您是否尝试过以管理员权限运行脚本?