行不会拆分成数组

Line wont split into array

从文件中获取一行并将其拆分时告诉我:

Traceback (most recent call last):
  File "C:\Users\service user\Desktop\cmd\main.py", line 94, in <module>
    start()
  File "C:\Users\service user\Desktop\cmd\main.py", line 66, in start
    e = f.split('/')
AttributeError: 'file' object has no attribute 'split'

我的代码在这里:

if split[0] == "/mount":
            extract(split[1])
            with open("info.m") as f:
                f.readlines()
                print f
                e = f.split('/')
                newlang = Conlang(e[0],e[1],e[2],e[3])
                f.close()
                print "Mounted as %s" % (e[0])

如果您知道它为什么不会分裂,我们将不胜感激。

也许你的意思是这样的:

with open("info.m") as f:
    # now we have a file object 'f', we want to iterate it's lines
    for line in f.readlines():
        print line
        e = line.split('/')
        # ... rest of code

此代码将 f 拆分为一个文件对象(也是一个迭代器)到一个行列表。然后它将迭代该列表并打印每一行(以及更多...)。