简体   繁体   English

Python .app不会像应读取的.txt文件一样

[英]Python .app doesn't read .txt file like it should

This question relates to this one: Python app which reads and writes into its current working directory as a .app/exe 这个问题与此有关: Python应用程序以.app / exe的形式读写当前工作目录

I got the path to the .txt file fine however now when I try to open it and read the contents it seems that it doesn't extract the data properly. 我找到了.txt文件的路径,但是现在当我尝试打开它并读取内容时,似乎无法正确提取数据。

Here's the relevant code: 以下是相关代码:

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
    # ...

These are the errors I'm getting: 这些是我得到的错误:

  • 30/09/2012 10:28:49.103 [0x0-0x4e04e].org.pythonmac.unspecified.main: for index, item in enumerate( lines ): # iterate through lines 30/09/2012 10:28:49.103 [0x0-0x4e04e] .org.pythonmac.unspecified.main:对于索引,枚举(行)中的项:#遍历行
  • 30/09/2012 10:28:49.103 [0x0-0x4e04e].org.pythonmac.unspecified.main: TypeError: 'NoneType' object is not iterable 30/09/2012 10:28:49.103 [0x0-0x4e04e] .org.pythonmac.unspecified.main:TypeError:'NoneType'对象不可迭代

I kind of understand what the errors mean however I'm not sure why they are being flagged up because if I run my script with it not in a .app form it doesn't get these errors and extracts the data fine. 我有点理解错误的含义,但是我不确定为什么要对它们进行标记,因为如果我不以.app形式运行脚本,它就不会得到这些错误并且不会提取数据。

You cannot read a file twice without resetting the read pointer. 如果不重置读取指针,则无法读取文件两次。 Moreover, your code actively prevents your file from being read properly. 此外,您的代码会主动阻止文件的正确读取。

Your code currently does this: 您的代码当前正在执行此操作:

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

The .read() statement can read the whole file into memory in one go, resulting in the read pointer being moved to the end. .read()语句可以一次将整个文件读取到内存中,从而导致读取指针移到末尾。 The loop over .readlines() will not return anything. .readlines()的循环将不返回任何内容。

But you also only run that code if your .read() call did not read anything . 但是,如果您的.read()调用未读取任何内容 ,则也仅运行该代码。 You are basically saying: If the file is empty, read the lines, otherwise do not read anything. 您基本上是在说:如果文件为空,请读取行,否则不读取任何内容。

In the end this means your getlines() function always returns None , later leading to the error you see. 最后,这意味着您的getlines()函数始终返回None ,稍后会导致您看到错误。

Loose the if len(f.read()) <= 0: altogether: 完全松开if len(f.read()) <= 0:

f= open(real_path + filename, 'r')  # open the file as an object

lines = {}                  # list to hold lines in the file
for line in f.readlines():  # loop through the lines

You then don't do anything much with lines = {} because for each line in your file you replace the lines variable: lines = line.split(" ") . 然后,您不需要对lines = {}做任何事情,因为对于文件中的每一行,您都替换lines变量: lines = line.split(" ") You probably meant to create a list instead, and append: 您可能打算创建一个列表,然后追加:

f= open(real_path + filename, 'r')  # open the file as an object

lines = []              # list to hold lines in the file
for line in f.readlines():  # loop through the lines
    # process line
    lines.append(line.split("    "))

Another tip: real_path = path[0: len(path) - 8] can be rewritten to real_path = path[:-8] . 另一个技巧: real_path = path[0: len(path) - 8]可以重写为real_path = path[:-8] You probably want to look into the os.path module to manipulate your paths though; 您可能想研究os.path模块来操纵您的路径。 I suspect a os.path.split() call would work better and more reliably for you there. 我怀疑os.path.split()调用在您那里会更好,更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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