我有一个简单的脚本,它解析文件并将其内容加载到数据库.我不需要UI,但是现在我提示用户使用raw_input哪个文件解析最不友好,特别是因为用户无法复制/粘贴路径.我想快速简便地向用户呈现文件选择对话框,他们可以选择文件,然后将其加载到数据库.(在我的用例中,如果他们碰巧选择了错误的文件,它将无法解析,即使它被加载到数据库也不会有问题.)
import tkFileDialog
file_path_string = tkFileDialog.askopenfilename()
Run Code Online (Sandbox Code Playgroud)
这段代码接近我想要的,但它留下一个恼人的空框架打开(无法关闭,可能是因为我没有注册一个关闭事件处理程序).
我不必使用tkInter,但由于它在Python标准库中,因此它是最快和最简单的解决方案的良好候选者.
什么是在没有任何其他UI的情况下在脚本中提示文件或文件名的快速简便方法?
我是使用Django的新手,我正在尝试开发一个用户可以上传大量excel文件的网站,然后将这些文件存储在媒体文件夹Webproject/project/media中.
def upload(request):
if request.POST:
form = FileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return render_to_response('project/upload_successful.html')
else:
form = FileForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('project/create.html', args)
Run Code Online (Sandbox Code Playgroud)
然后,文档将与列表中的任何其他文档一起显示在列表中,您可以单击该文档,它将显示有关它们的基本信息以及它们已上载的文件的名称.从这里我希望能够使用以下链接再次下载相同的excel文件:
<a href="/project/download"> Download Document </a>
Run Code Online (Sandbox Code Playgroud)
我的网址是
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
template_name="project/project.html")),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model=Post, template_name="project/post.html")),
url(r'^upload/$', upload),
url(r'^download/(?P<path>.*)$', serve, {'document root': settings.MEDIA_ROOT}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误,serve()得到了一个意外的关键字参数'document root'.谁能解释如何解决这个问题?
要么
说明如何使用上传的文件进行选择和提供
def download(request):
file_name = #get the filename of desired excel file
path_to_file = #get the path of desired excel file …Run Code Online (Sandbox Code Playgroud) 我的django网络应用程序制作并保存docx,我需要让它可下载.我用简单render_to_response如下.
return render_to_response("test.docx", mimetype='application/vnd.ms-word')
Run Code Online (Sandbox Code Playgroud)
但是,它会引发错误 'utf8' codec can't decode byte 0xeb in position 15: invalid continuation byte
我无法将此文件作为静态服务,所以我需要找到一种方法来为它提供服务.真的很感激任何帮助.