在別人告訴我再搜索一下網頁之前,我已經搜索了一個多小時了。無類型對象不可迭代
所以我的任務需要我使用一個導入的模塊,其中包含一個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