誰かが私に Web をもう少し検索するように言う前に、私はすでに 1 時間以上検索しています。
そのため、私の課題では、メイン モジュールの selectedFileCopy のファイルを開く safeOpen 関数を含むインポートされたモジュールを使用する必要があります。しかし、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))