【发布时间】:2012-07-04 13:05:54
【问题描述】:
我的views.py文件代码:
#!/usr/bin/python
from django.template import loader, RequestContext
from django.http import HttpResponse
#from skey import find_root_tags, count, sorting_list
from search.models import Keywords
from django.shortcuts import render_to_response as rr
def front_page(request):
if request.method == 'POST' :
from skey import find_root_tags, count, sorting_list
str1 = request.POST['word']
fo = open("/home/pooja/Desktop/xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
for name, count1 in list:
s = Keywords(file_name=name,frequency_count=count1)
s.save()
fo.close()
list1 = Keywords.objects.all()
t = loader.get_template('search/results.html')
c = RequestContext({'list1':list1,
})
return HttpResponse(t.render(c))
else :
str1 = ''
list = []
template = loader.get_template('search/front_page.html')
c = RequestContext(request)
response = template.render(c)
return HttpResponse(response)
我在函数find_root_tags(file,str1,i) 中发送的变量“文件”有一个xml 文件名。该文件存在于我的桌面上,并且此代码编写在我的 django 应用程序的 views.py 文件中,因此无法打开该文件。我如何打开该文件,因为 xml.txt 包含要读取然后打开的相似文件名。简而言之,我怎样才能将文件参数发送为:
file1 = '/home/pooja/Desktop/<filename>'
这里<filename> 等于存储在变量file 中的值,最终可以将其称为:
find_root_tags(file1, str1, i)
/////////////////////////////////////// //////////////////////////////////////////////p>
澄清:
1)请参考变量“file”,您可以在其中看到我正在存储xml.txt的读取内容。
2)xml.txt 包含 xml 文件名。 views.py 文件是 django 应用程序的一部分,由于它们存在于桌面上,因此无法打开这些文件。
3) 我的问题是如何修改和发送包含文件名的文件变量,并附加它的绝对路径:
'/home/pooja/Desktop/filename'
这样做会打开桌面上的文件。
【问题讨论】:
标签: python django linux file ubuntu-10.04