2016-03-22 3 views
1

私は奇妙な問題に直面しています。私のdjangoフレームワークは、テキストファイルを読み込んだ後にデータを取得するためのテンプレートから引数を取得します。テスト中は完璧な仕事をしています。私は、ファイルが存在しているはず、私のPythonスクリプトは、それを読むことができますがデータを取得するためにdjangoがテキストファイルを読み取る

IOError at /search/ 
[Errno 2] No such file or directory: 

:しかし、ときに私は、私は、ブラウザのいずれかの次のエラーを取得しています、同時に2つのブラウザから同じ引数を渡しています。だから私は過去にこのような問題に直面している人がいると思っています!! Djangoでデータを取得するためのファイルを処理する最良の方法は何ですか?私のDjangoプロジェクト

def search(request): 
if 'searchterm' in request.GET and request.GET['searchterm']: 
    searchterm=request.GET['searchterm'] 
    searchtype= request.GET['searchtype'] 
    if len(searchterm)>0: 
     pepfile = open(settings.BASE_DIR+'/filetoread/ReportBook_active_optimization.csv','r') 
     contextres ={} 
     concenrange={} 
     for line in pepfile: 
      data=line.strip() 
      if not data.startswith('txtPeptideID'): 
       info= data.split('\t') 
       acclist=[] 

       if searchtype =='Protein' and (str(searchterm)).lower() in info[2].lower(): 

        for items in info[2].split('|'): 
         if (str(searchterm)).lower() in (items.strip()).lower(): 
          itemsindex=(info[2].split('|')).index(items) 
          acclist.append((info[3].split('|'))[itemsindex]) 

          transcountag6490=0 
          transpath=settings.BASE_DIR+'/tranisitionfilestoread' 
          curr_dir = os.getcwd() 
          os.chdir(transpath) 

          with open('transitions_6490_Agilent.csv', 'r') as transcountag6490file: 
           for line in transcountag6490file: 
            if str(pepid) in line: 
             transcountag6490=1 
            else: 
             transcountag6490=0 
          transcountag6490file.close() 



     return render(request, 'resultform.html', {'contextresultinfo': contextres, 'query': searchterm}) 
    else: 
     return render(request, 'index.html', {'error': True}) 
else: 
    return render(request, 'index.html', {'error': True}) 

エラーの完全なトレースのコードの おかげ

一部:

IOError at /search/ 

[Errno 2] No such file or directory: 'transitions_6490_Agilent.csv' 

Request Method:  GET 
Request URL: http:/127.0.0.1:8000/:8000/search/?searchtype=Any&  searchterm=PEP2012090602 
Django Version:  1.8.11 
Exception Type:  IOError 
Exception Value:  

[Errno 2] No such file or directory: 'transitions_6490_Agilent.csv' 

Exception Location:  /home/paul/Desktop/djangoproject/trackerdatabase/src/trackerapp/views.py in search, line 188 
Python Executable: /usr/bin/python 
Python Version:  2.7.6 
Python Path:  

['/home/paul/Desktop/djangoproject/trackerdatabase/src', 
'/usr/local/lib/python2.7/dist-packages/setuptools-20.3.1-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/pip-8.1.1-py2.7.egg', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/pymodules/python2.7', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 
+1

これは実際に実装に固有のものです。コードを共有する必要があります。そうしないと、他の人があなたを助けることができるように十分な情報が得られませんでした。 –

+0

私は自分のプロジェクトのコード部分を共有しています – Paul85

+0

あなたはあなたのコードのどこかのファイル/ディレクトリに間違ったパスを定義します。完全なエラースタックトレースを表示できますか? – iulian

答えて

1

私は他のSOのメンバーによって容易に見つけるための答えとしてそれをここに残しておきます。

os.chdirを作成する必要はありません。ファイルへのパス全体を直接open()コールに渡すだけです。

file_path = os.path.join(settings.BASE_DIR, 'tranisitionfilestoread', 'transitions_6490_Agilent.csv') 
with open(file_path, 'r') as f: 
    # do stuff 
+0

私はあなたの答えを受け入れています – Paul85

関連する問題