Python 从文本文件web2py读取

Python 从文本文件web2py读取,python,file,web2py,Python,File,Web2py,我对web2py有意见。我有一个名为deffals.txt的文本文件,它位于modules文件夹中。我尝试使用open(“deffals.txt”)(在与deffals.txt位于同一控制器中的模块中)从中读取,但我得到了错误: Traceback (most recent call last): File "/home/jordan/web2py/gluon/restricted.py", line 212, in restricted exec ccode in environmen

我对web2py有意见。我有一个名为deffals.txt的文本文件,它位于modules文件夹中。我尝试使用
open(“deffals.txt”)
(在与deffals.txt位于同一控制器中的模块中)从中读取,但我得到了错误:

Traceback (most recent call last):
 File "/home/jordan/web2py/gluon/restricted.py", line 212, in restricted
   exec ccode in environment
File "/home/jordan/web2py/applications/randommotif/controllers/default.py", line 67,     in <module>
 File "/home/jordan/web2py/gluon/globals.py", line 188, in <lambda>
self._caller = lambda f: f()
File "/home/jordan/web2py/applications/randommotif/controllers/default.py", line 13, in index
  defaultData = parse('defVals.txt')
File "applications/randommotif/modules/defaultValParser.py", line 6, in parse
 lines = open(fileName)
IOError: [Errno 2] No such file or directory: 'defVals.txt'
回溯(最近一次呼叫最后一次):
文件“/home/jordan/web2py/glion/restricted.py”,第212行,受限
环境中的执行代码
文件“/home/jordan/web2py/applications/randomotif/controllers/default.py”,第67行,在
文件“/home/jordan/web2py/glion/globals.py”,第188行,在
self.\u caller=lambda f:f()
文件“/home/jordan/web2py/applications/randomotif/controllers/default.py”,第13行,索引中
defaultData=parse('deffals.txt')
文件“applications/randommotif/modules/defaultValParser.py”,第6行,在parse中
行=打开(文件名)
IOError:[Errno 2]没有这样的文件或目录:“deffals.txt”
我做错了什么?我应该在哪里放置deffals.txt

我正在使用Ubuntu 12.10

谢谢

约旦

更新:

这是defaultValParser.py的源代码:

import itertools
import string
import os
from gluon import *
from gluon.custom_import import track_changes; track_changes(True)

#this returns a dictionary with the variables in it.
def parse(fileName):
    moduleDir = os.path.dirname(os.path.abspath('defaultValParser.py'))
    filePath = os.path.join(moduleDir, fileName)
    lines = open(filePath, 'r')
    #remove lines that are comments. Be sure to remove whitespace in the beginning and end of line
    real = filter(lambda x: (x.strip())[0:2] != '//', lines)
    parts = (''.join(list(itertools.chain(*real)))).split("<>")
    names = map(lambda x: (x.split('=')[0]).strip(), parts)
    values = map(lambda x: eval(x.split('=')[1]), parts)
    return dict(zip(names, values))
导入itertools
导入字符串
导入操作系统
从胶子进口*
从gluon.custom\u导入轨迹\u更改;跟踪更改(真)
#这将返回一个包含变量的字典。
def解析(文件名):
moduleDir=os.path.dirname(os.path.abspath('defaultValParser.py'))
filePath=os.path.join(moduleDir,文件名)
行=打开(文件路径“r”)
#删除作为注释的行。确保删除行首和行尾的空白
实数=过滤器(lambda x:(x.strip())[0:2]!=“/”,行)
parts=(“”.join(list(itertools.chain(*real))).split(“”)
名称=映射(lambda x:(x.split('=')[0]).strip(),parts)
值=映射(λx:eval(x.split('=')[1]),零件)
返回dict(zip(名称、值))
如果我导入它并从终端调用它(前提是我注释掉gluon导入),它工作正常,但是如果我从web2py控制器调用它,它将完全失败:

Traceback (most recent call last):
  File "/home/jordan/web2py/gluon/restricted.py", line 212, in restricted
   exec ccode in environment
  File "/home/jordan/web2py/applications/randommotif/controllers/default.py", line 71, in <module>
  File "/home/jordan/web2py/gluon/globals.py", line 188, in <lambda>
  self._caller = lambda f: f()
  File "/home/jordan/web2py/applications/randommotif/controllers/default.py", line 17, in index
  defaultData = parse('defVals.txt')
  File "applications/randommotif/modules/defaultValParser.py", line 6, in parse
 IOError: [Errno 2] No such file or directory: 'defVals.txt'
回溯(最近一次呼叫最后一次):
文件“/home/jordan/web2py/glion/restricted.py”,第212行,受限
环境中的执行代码
文件“/home/jordan/web2py/applications/randomotif/controllers/default.py”,第71行,在
文件“/home/jordan/web2py/glion/globals.py”,第188行,在
self.\u caller=lambda f:f()
文件“/home/jordan/web2py/applications/randomotif/controllers/default.py”,第17行,索引中
defaultData=parse('deffals.txt')
文件“applications/randommotif/modules/defaultValParser.py”,第6行,在parse中
IOError:[Errno 2]没有这样的文件或目录:“deffals.txt”

使用基于模块路径的绝对路径:

moduledir = os.path.dirname(os.path.abspath('__file__'))

# ..
defaultData = parse(os.path.join(moduledir, 'defVals.txt'))
\uuuu file\uuuu
是当前模块的文件名,使用的
.dirname()
提供模块所在的目录。我使用了
.abspath()
来确保您始终拥有模块文件的绝对路径,避免了一些您可能遇到的问题


moduledir
是模块中的一个全局函数。

如果我从终端调用解析函数,它现在可以正常工作。但如果我从web2py控制器调用它,它仍然会失败。我将发布defaultValParser.py的(更新的)源代码,以及我得到的错误。@JordanForce:您没有使用我的答案:-)使用
\uuuu文件\uuuuuuuuu
并将
moduledir
放在全局级别。我不是想让你用模块的文件名来替换
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,而
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu