使用Python3瓶子访问上载的文件

使用Python3瓶子访问上载的文件,python,python-3.x,file-upload,bottle,Python,Python 3.x,File Upload,Bottle,我为我的大学做一个项目。我们已经学习了python的基础知识。任务是创建一个小程序,读取fastq文件并对其进行分析。我想用瓶子创建一个基于html的用户界面会很好。但是没有真正的服务器。 我的问题是:我不知道如何访问上传的文件。代码不会创建新目录。我也不知道在哪里可以找到“上传”的文件,也不知道服务器站点上的新功能如何获取该文件并使用它。 我读过以下网站: 等等 此外,当我尝试上载文件时,错误使我烦恼,错误是文件已经存在。有一个类UploadFile,具有函数save,它有一个覆盖选项,但

我为我的大学做一个项目。我们已经学习了python的基础知识。任务是创建一个小程序,读取fastq文件并对其进行分析。我想用瓶子创建一个基于html的用户界面会很好。但是没有真正的服务器。
我的问题是:我不知道如何访问上传的文件。代码不会创建新目录。我也不知道在哪里可以找到“上传”的文件,也不知道服务器站点上的新功能如何获取该文件并使用它。
我读过以下网站:


等等

此外,当我尝试上载文件时,错误使我烦恼,错误是文件已经存在。有一个类
UploadFile
,具有函数
save
,它有一个覆盖选项,但我不知道如何实现它

瓶试验。py:

from bottle import route, run, template, static_file, request, response, url, default_app, get, post, FileUpload
import bottle
import os
# Aufrufen der Hauptseite
@route('/')
def index():
return template('main_template')
# Einbinden unterschiedlicher Dateien z.B. Bilder oder CSS-Files
@route('/static/style/<filepath:re:.*\.css>')
def server_static(filepath):
return static_file(filepath, root='static/style')


@route('/static/images/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
    return static_file(filepath, root="static/images")

@route('/static/sonstige-bilder/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
    return static_file(filepath, root='static/sonstige-bilder')
# Formularabfrage
@route('/repeat', method='POST')
def do_login():
    username = request.forms.get('username')
    password = request.forms.get('password')
    if username == 'arsenij' and password == '1234':
        return "<p>Your login information was correct.</p>"
    else:
        return "<p>Login failed.</p>"

@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.fastq'):
    return 'File extension not allowed.'

save_path = '/tmp/(category)'
if not os.path.exists(save_path):
    os.makedirs(save_path)

file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
print(request.files.get('upload'))
return 'File uploaded'

if __name__ == '__main__':
    bottle.debug(True)
    bottle.run(host='0.0.0.0', port=8080,  reloader=True)
从瓶子导入路径、运行、模板、静态文件、请求、响应、url、默认应用程序、获取、发布、文件上载
进口瓶
导入操作系统
#奥夫鲁芬·德豪普塞特酒店
@路由(“/”)
def index():
返回模板(“主模板”)
#Einbinden unterschiedlicher Dateien z.B.Bilder订单CSS文件
@路由(“/static/style/”)
def服务器_静态(文件路径):
返回静态文件(filepath,root='static/style')
@路由(“/static/images/”)
def img(文件路径):
返回静态文件(filepath,root=“static/images”)
@路线('/static/sonstige-bilder/')
def img(文件路径):
返回静态文件(filepath,root='static/sonstige-bilder')
#处方集
@路由('/repeat',method='POST')
def do_login():
username=request.forms.get('username')
password=request.forms.get('password')
如果用户名='arsenij'和密码='1234':
return“您的登录信息正确。

” 其他: 返回“登录失败。

” @路由('/upload',method='POST') def do_upload(): category=request.forms.get('category') upload=request.files.get('upload') name,ext=os.path.splitext(upload.filename) 如果ext不在('.fastq'): 返回“不允许文件扩展名” 保存路径='/tmp/(类别)' 如果操作系统路径不存在(保存路径): os.makedirs(保存路径) file_path=“{path}/{file}”。格式(path=save_path,file=upload.filename) upload.save(文件路径) 打印(request.files.get('upload')) 返回“已上载文件” 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': beggle.debug(True) 瓶子运行(主机=0.0.0.0',端口=8080,重新装载器=True)
main_template.tpl

<form action="/upload" method="post" enctype="multipart/form-data">
Category:      <input type="text" name="category" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>

类别:
选择一个文件:
显示了如何执行此操作:

upload.save(file_path, overwrite=True)

仅供参考,我建议您通过
pylint
运行代码。