简体   繁体   English

为什么我不能在 python 中写入文件

[英]Why can't I write to a file in python

I have a python program that generates palindromes and writes them to a file.我有一个 python 程序可以生成回文并将它们写入文件。 But then I wanted to modify the code to generate only palindromes that were meaningful (ie in the dictionary).但是后来我想修改代码以仅生成有意义的回文(即在字典中)。 So I looked up a .txt version of an English dictionary that I could use in my python program on the internet.因此,我在互联网上查找了可以在我的 python 程序中使用的英语词典的 .txt 版本。 So the problem is, before I added the code that read the dictionary file, the palindrome program was working perfectly and writing the palindromes to the file I specified, but when I added that if statement (that checks whether the palindrome generated is in the dictionary) to the program, it stopped writing to the file.所以问题是,在我添加读取字典文件的代码之前,回文程序运行良好并将回文写入我指定的文件,但是当我添加if语句时(检查生成的回文是否在字典中) 到程序,它停止写入文件。 Here is the code:这是代码:

import itertools, math
from string import ascii_uppercase

def vowel(string):
    return any(v in string for v in 'AEIOU')
         
MAX_LEN = 8
FILENAME = "Palindrome_Dict.txt"
NUMBERS = ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE', 'THIRTEEN', 'FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN', 'NINETEEN', 'TWENTY']
DICT = []

f = open("english3.txt", 'r')
DICT = f.readlines()
DICT = list(map(lambda w: w[:-2], DICT))
f.close()
    
with open(FILENAME, 'w+') as file:
    file.writelines(["ALL THE POSSIBLE PALINDROMES\n", "By: Ayinde-Oladeinde Ayoife (a.k.a Index)\n"])
    
    for n in range(3, MAX_LEN):
         list = []
         
         sentence = f" {NUMBERS[n-1]} LETTER PALINDROMES "
         file.writelines(['\n'+sentence+'\n', '-'*len(sentence)+'\n'])
         
         l = math.ceil(n/2)
         for w in itertools.product(*[ascii_uppercase]*l):
            word = ''.join(w)
            
            if word[0] not in list:
                file.writelines(['\n'+f"--{word[0]}--\n"])
                list.append(word[0])
            
            word = word + word[::-1] if n % 2 == 0 else word + word[:-1][::-1]
            '''if word.lower() in DICT:
                print(word)

file.writelines([word.capitalize()+'\n'])'''
            file.writelines([word.capitalize()+'\n'])

As you can see from the above code that i commented the if statement that checked whether the word was in the dictionary list.从上面的代码可以看出,我注释了检查单词是否在字典列表中的 if 语句。 And this code works as expected but once you uncomment that if statement and you check the Palindrome_Dict.txt file you'll see that the file is completely empty.并且此代码按预期工作,但是一旦您取消注释该 if 语句并检查 Palindrome_Dict.txt 文件,您将看到该文件完全为空。 I've tried everything i could and i really need your help in fixing this bug.我已经尽我所能,我真的需要你的帮助来修复这个错误。 Thanks in advance :)提前致谢 :)

Firstly, I would like to thank everyone that helped me with the question.首先,我要感谢所有帮助我解决这个问题的人。 And I want to say that I've found a temporary fix to the problem and I found the solution on this website https://www.pythonanywhere.com/forums/topic/860/ which mentioned that file.flush() immediately writes the preceding .write() or .writelines() statement to the specified file, which is what I did.我想说我已经找到了解决问题的临时方法,并且我在这个网站https://www.pythonanywhere.com/forums/topic/860/上找到了解决方案,其中提到了file.flush()立即写道前面的.write().writelines()语句到指定的文件,这就是我所做的。 And like that, the problem was solved.就这样,问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM