错误:python中的文件i/o

2024-04-25 00:43:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我的视图.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)中发送的变量“file”有一个xml文件名。这个文件出现在我的桌面上,这段代码是用视图.py我的django应用程序的文件,因此无法打开该文件。我怎么能打开那个文件因为xml.txt文件包含要读取然后打开的类似文件名。简而言之,如何将文件参数发送为:

^{pr2}$

这里<filename>等于存储在变量file中的值,最后可以将其调用为:

find_root_tags(file1, str1, i)

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

澄清:

1)请参考变量“file”,在该变量中可以看到我存储的是xml.txt文件. 在

(二)xml.txt文件包含xml文件名。这个视图.py文件是django应用程序的一部分,它无法打开这些文件,因为它们存在于桌面上。在

3)我的问题是如何修改和发送包含文件名的文件变量及其绝对路径,即:

'/home/pooja/Desktop/filename'

通过这样做,它将打开桌面上的文件。在


Tags: 文件djangofromimporttxtrequestcounttags
2条回答

试试这个:

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

如果我理解您的问题,文件/home/pooja/Desktop/xml.txt包含文件名,每行一个,相对于/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]))

Asides

首先,请注意,我已经将您的count.__len__替换为len(count)。在Python中,魔术方法,即__xxxx__形式的方法不直接调用。它们被定义为由其他机制调用。对于len方法,当您使用len()内置时,会在内部调用它。在

其次,请注意,如果xml.txt中的行数少于len(count),那么一旦您读取了文件的所有行,fo.readline将引发一个异常。在Python中读取文件所有行的通常方法是:

^{pr2}$

最后,为了确保无论发生什么情况,也就是说,即使在读取文件时引发异常,也可以使用with statement。在

所以在你的例子中,你可以做一些类似的事情:

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]))

相关问题 更多 >