4

web2py で検索エンジン スクリプト用のデータベース (.txt 形式) を開く必要があります。

pythonanywhere の無料版を使用しているため、オンライン データベースにアクセスできません。

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

データベースを「プライベート」ファイルフォルダーにアップロードしましたが、どうすればアクセスできるのでしょうか。簡単な質問のように見えますが、うまくいかないようです。

私はこのようなものが必要です:

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

良い解決策は何ですか?

4

1 に答える 1

7

これは次のことを行う必要があります。

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

于 2013-08-11T07:25:31.333 に答える