简体   繁体   中英

Why can't I write to a file in python

I have a python program that generates palindromes and writes them to a file. 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. 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. 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. 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. 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. And like that, the problem was solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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