0

私の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)

関数で送信している変数「file」にfind_root_tags(file,str1,i)は、xmlファイルの名前が付いています。このファイルはデスクトップにあり、このコードはdjangoアプリのviews.pyファイルに記述されているため、そのファイルを開くことができません。xml.txtには、読み取ってから開く同様のファイル名が含まれているため、そのファイルを開くにはどうすればよいですか。つまり、ファイル引数を次のように送信するにはどうすればよいですか。

file1 = '/home/pooja/Desktop/<filename>'

ここで<filename>は、変数に格納されている値に等しく、filefinallはそれを次のように呼び出すことができます。

find_root_tags(file1, str1, i)

////////////////////////////////////////////////// //////////////////////////////////////////

明確化:

1)xml.txtの読み取りコンテンツを保存していることがわかる変数「file」を参照してください。

2)xml.txtにはxmlファイル名が含まれています。views.pyファイルはdjangoアプリの一部であり、デスクトップに存在するため、これらのファイルを開くことができません。

3)私の質問は、ファイル名を含むファイル変数に絶対パスを追加して送信する方法です。これは次のとおりです。

'/home/pooja/Desktop/filename'

これを行うことにより、デスクトップに存在するファイルが開きます。

4

2 に答える 2

1

これを試して:

pathh='/home/pooja/Desktop/'      #set the base path       
fo = open("/home/pooja/Desktop/xml.txt")
for i in range(len(count)):     #use len(count)
    file = fo.readline()
    file = file.strip()          #use strip()
    find_root_tags(pathh+file,str1,i) #base path+file
    mylist.append((file,count[i]))   #using 'list' as a variable name is not good
于 2012-07-04T13:20:33.523 に答える
0

したがって、私があなたの質問を理解している場合、ファイル/home/pooja/Desktop/xml.txtにはファイル名が1行に1つずつ含まれており/home/pooja/Desktop/、フルパス名をに渡す必要がありますfind_root_tags

Pythonでは、を使用+して文字列を連結できるため、次のようなことができます。

files_path = "/home/pooja/Desktop/"

for i in range(len(count)):

    file = fo.readline()
    file = file.rstrip('\n')
    find_root_tags(files_path + file, str1, i) 

    list.append((file,count[i]))

傍白

count.__len__まず、あなたをに置き換えたことに注意してくださいlen(count)。Pythonでは、魔法のメソッド、つまりフォームのメソッドは__xxxx__直接呼び出されません。それらは、他のメカニズムによって呼び出されるように定義されています。lenメソッドの場合、len()組み込みを使用すると内部的に呼び出されます。

次に、の行数が。xml.txtより少ない場合、ファイルのすべての行を読み取ると例外が発生することに注意してください。Pythonでファイルのすべての行を読み取る通常の方法は次のとおりです。len(count)fo.readline

my_file = open("/path/to/file", 'r')
for line in my_file:
    # do something with your line

最後に、何が起こってもファイルが閉じていることを確認します。つまり、ファイルの読み取り中に例外が発生した場合でも、withステートメントを使用できます。

したがって、あなたの例では、次のようなことをします。

files_path = "/home/pooja/Desktop/"
with open("/path/to/file", 'r') as my_file:
    for file in my_file:
        file = file.rstrip('\n')
        find_root_tags(files_path + file, str1, i) 

        list.append((file,count[i]))
于 2012-07-04T13:14:43.383 に答える