这个问题与这个问题有关:Python app which reads and writes into its current working directory as a .app/exe
我得到了 .txt 文件的路径,但是现在当我尝试打开它并读取内容时,它似乎没有正确提取数据。
以下是相关代码:
def getLines( filename ):
path = Cocoa.NSBundle.mainBundle().bundlePath()
real_path = path[0: len(path) - 8]
print real_path
f = open(real_path + filename, 'r') # open the file as an object
if len(f.read()) <= 0:
lines = {} # list to hold lines in the file
for line in f.readlines(): # loop through the lines
line = line.replace( "\r", " " )
line = line.replace( "\t", " " )
lines = line.split(" ") # segment the columns using tabs as a base
f.close() # close the file object
return lines
lines = getLines( "raw.txt" )
for index, item in enumerate( lines ): # iterate through lines
# ...
这些是我得到的错误:
- 30/09/2012 10:28:49.103 [0x0-0x4e04e].org.pythonmac.unspecified.main: for index, item in enumerate(lines): # 遍历行
- 30/09/2012 10:28:49.103 [0x0-0x4e04e].org.pythonmac.unspecified.main: TypeError: 'NoneType' 对象不可迭代
我有点理解这些错误的含义,但是我不确定为什么要标记它们,因为如果我使用它而不是以 .app 形式运行我的脚本,它就不会出现这些错误并且可以很好地提取数据。