简体   繁体   中英

Opening a text file from the private folder in web2py

I need to open a database (in .txt format) for my search engine script in web2py.

I can not access the online database, because I use the free version of pythonanywhere.

import urllib
infile=urllib.urlopen('http://database.net')
for line in infile:

Now I uploaded the database to the "private" file folder and I wonder how get access it. It looks like a simple question, but I can't seem to work it.

I need something like this:

infile = open('searchapp/private/database.txt')
for line in infile:

What is a good solution?

This should do:

import os
infile = open(os.path.join(request.folder, 'private', 'database.txt'))
for line in infile:

http://www.web2py.com/books/default/chapter/29/04/the-core#request

http://docs.python.org/2/library/os.path.html#os.path.join

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM