在别人告诉我再搜索一下网页之前,我已经搜索了一个多小时了。无类型对象不可迭代
所以我的任务需要我使用一个导入的模块,其中包含一个safeOpen函数,用于打开主模块的文件selectiveFileCopy。但是,当我调用safeOpen函数时,它说我试图打开的文件是一个None类型,因此不可迭代。我不确定这是为什么。
下面是一些代码:
def safeOpen(prompt, openMode, errorMessage):
while True:
try:
open(input(prompt),openMode)
return
except IOError:
return(errorMessage)
def selectivelyCopy(inputFile,outputFile,predicate):
linesCopied = 0
for line in inputFile:
outputFile.write(inputFile.predicate)
if predicate == True:
linesCopied+=1
return linesCopied
inputFile = fileutility.safeOpen("Input file name: ", "r", " Can't find that file")
outputFile = fileutility.safeOpen("Output file name: ", "w", " Can't create that file")
predicate = eval(input("Function to use as a predicate: "))
print(type(inputFile))
print("Lines copied =",selectivelyCopy(inputFile,outputFile,predicate))
错误发生在哪条线上? – Cameron 2012-04-15 21:10:56