尝试将输出写入文件时出现错误

时间:2019-05-01 19:32:34

标签: python file input output spell-checking

在我创建的这个拼写检查程序中,尝试写入输出文件时似乎出现"File did not open"错误。创建了文件,但未写入输出" <_io.TextIOWrapper name='f.txt' mode='w' encoding='cp1252'> "

我试图寻找解决方案。

print('Spell checking program for Exam 3 lab')

inputFile  = input('Enter the name of the file to input from: ')

outputFile = input('Enter the name of the file to output to: ')

f = open("linuxwords.txt", "r")

sent = open(inputFile+ '.txt', "r")

butt = open(outputFile+'.txt', 'w')

word = sent.readline()

print ("mispelled words are:")

while word:

    word = word.lower()
    success = False
    x = word.split()
    y=len(x)
    for i in x:
        success = False
        f = open("linuxwords.txt", "r")
        line = f.readline()
        while line:
            if i == line.strip():
                success = True
                break
            line = f.readline()
        f.close()
        if success == False:
            print (i)
    word = sent.readline()
    with open(outputFile+'.txt', 'w') as f:
        f.write(str(butt))
    try:
        f = open(outputFile)
        f.write(i)
    except:
         print('The file',outputFile, 'did not open.')


sent.close()

考试3实验室的拼写检查程序 输入要从中输入文件的名称:spw 输入文件名称以输出到:f 拼写错误的单词是: 德克斯 克里斯 贬义者 惠斯 第一 文件f没有打开。

1 个答案:

答案 0 :(得分:3)

f = open(outputFile)
f.write(i)

您正在打开文件进行读取,然后尝试对其进行写入