0

だから私はそれをテストするためにweb.pyのアップロードと保存のガイドに従っていますが、[Errno 2] No such file or directory :< helpというエラーが表示され続けます

これは私のコードです

import web

urls = (
    '/hello', 'index',
    '/hello/upload', 'upload'
)
app = web.application(urls, globals()) # handels http request  that aks for urls 
# the base ells lpthw.web to use the templates/layout.html file as the base template for all the other templates
render = web.template.render('templates/', base="layout")
# must use port 127.0.0.1:5000
class index(object):

    def GET(self):
        return render.hello_form()

    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)

class upload(object):

    def POST(self):
        x = web.input(files={})

        filedir = '/project/webtest/templates'  # directory were you want to save the file 
        if 'files' in x:  # check if the file -object is created
            filepath=x.files.filename.replace('\\','/') # replace the windows -style slashes with linux ones
            filename=filepath.split('/')[-1] #splits the and chooses the last part (the filename with extension
            fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored
            fout.write(x.files.file.read()) #writes the uploaded file to the newly created file.            
            fout.close() #closes the file 
        else:
            return "Error no file" 


if __name__ == "__main__":
    app = web.application(urls, globals()) 
    app.run() 

助けてthx

4

2 に答える 2

0
import web

urls = ('/upload', 'Upload')

class Upload:
def GET(self):
    web.header("Content-Type","text/html; charset=utf-8")
    return view.upload()
def POST(self):
    x = web.input(myfile={})
    filedir = '/path/where/you/want/to/save' # change this to the directory you want to store the file in.
    if 'myfile' in x: # to check if the file-object is created
        filepath=x.myfile.filename.replace('\\','/') 
        filename=filepath.split('/')[-1] 
        fout = open(filedir +'/'+ filename,'w') 
        fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
        fout.close() # closes the file, upload complete.
    raise web.seeother('/upload')


if __name__ == "__main__":
   app = web.application(urls, globals()) 
   app.run()


upload.html
<html>
 <head>
 <title>File upload</title>
 </head>
 <body>
 <form method="POST" enctype="multipart/form-data" action="">
  <input type="file" name="myfile" /><br/>
  <input type="submit" />
 </form>
 </body>
 </html>

参考までにhttp://webpy.org/cookbook/storeupload/

于 2013-03-07T10:12:19.847 に答える
0

私は自分で同じ問題に遭遇し、ここでそれを尋ねようとしました。しかし、誰も答えません。

最後に、何が問題なのかを理解したので、あなたと共有したいと思います!

この部分:filedir = '/project/webtest/templates'は絶対パスである必要があります。

そして、それは既存のディレクトリである必要があります(少なくとも私のトレイルでは、既存のディレクトリである必要があります。そうしないと、投稿したのと同じエラーが表示されます)。アップロードされたファイルをコピーして作成するため、ファイルはエキサイティングである必要はありません。

たとえば、私の mac では、それ'/Users/J/pythonex/projects/gothonweb/docs'は であり、既存のディレクトリです。既存のディレクトリでない場合は、同じエラー メッセージが表示されます。

最後に、最もトリッキーな部分です。私のMacでは、アップロードされたファイルは実際にはディスクのその正確なディレクトリに保存されています。しかし、ファインダーを再起動するまで、ファインダーにそれらが表示されません。なぜだかわかりません。しかし、私のコンピューターの場合はそうです。

于 2015-03-08T13:35:33.327 に答える