【问题标题】:Error: file i/o in python错误:python 中的文件 i/o
【发布时间】: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>'

这里&lt;filename&gt; 等于存储在变量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


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      因此,如果我理解您的问题,文件 /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]))
      

      旁白

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

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

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

      最后,为了确保无论发生什么都关闭您的文件,即即使在您读取文件时引发异常,您也可以使用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]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-28
        • 2012-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        相关资源
        最近更新 更多