django 读取文本文件以检索数据
django reading text file to retrieve data
我遇到了一个奇怪的问题。我的 django 框架从模板获取参数以在读取文本文件后检索数据。在测试期间,它做得很完美。但是当我同时从两个浏览器传递相同的参数时,其中一个浏览器出现以下错误:
IOError at /search/
[Errno 2] No such file or directory:
尽管我知道文件在那里并且我的 python 脚本可以读取它。所以我想知道过去有人遇到过这种问题!!在 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']
我会把它留在这里作为答案,以便其他 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
我遇到了一个奇怪的问题。我的 django 框架从模板获取参数以在读取文本文件后检索数据。在测试期间,它做得很完美。但是当我同时从两个浏览器传递相同的参数时,其中一个浏览器出现以下错误:
IOError at /search/
[Errno 2] No such file or directory:
尽管我知道文件在那里并且我的 python 脚本可以读取它。所以我想知道过去有人遇到过这种问题!!在 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']
我会把它留在这里作为答案,以便其他 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