为什么我不能手动调用“file”迭代器上的“next”?

2024-06-28 10:52:01 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的Python代码。如果这行有:,我想跳过这行和下一行。我尝试使用file.next,因为file是一个迭代器。为什么不起作用?你知道吗

file=open("example.txt","r") //output of ls -R

currentDirectory=""
for line in file:
    if ":" in line:
        currentDirectory=line[:-1]
        file.next() # Error in this line
        continue
    A=line.split()
    print(A)
    print(len(A))
    print('insert into files set name=${name}, inode=${inode}, isDir=${isDir},size=${size}'
    .format(inode=A[0],name=A[9],isDir="d" in A[1],size=A[5]))

错误:

Traceback (most recent call last):
  File "ls-to-sql.py", line 7, in <module>
    file.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next

我相信有很多其他的方法来实现同样的逻辑。问题是:为什么这不起作用。如果有其他的迭代方法来解决它。你知道吗


Tags: 方法代码nameinsizeexamplelineopen