试图将字符串写入临时文件

时间:2014-03-14 03:46:33

标签: python python-3.x file-io gzip temporary-files

我正在制作一个包含20个问题的游戏。在代码中我创建了一个临时文件来跟踪用户的问题。这是代码:

import random
import turtle  
import tempfile  
import gzip  

def getAnswer():  
    obj = random.randrange(5)  
    if obj == 0:  
        infile1 = open("appleyes.txt", "r")  
        infile2 = open("applecanbe.txt", "r")  
        answer = "apple"  
    elif obj == 1:  
        infile1 = open("dogyes.txt", "r")  
        infile2 = open("dogcanbe.txt", "r")  
        answer = "dog"  
    elif obj == 2:  
        infile1 = open("carrotyes.txt", "r")  
        infile2 = open("carrotcanbe.txt", "r")  
        answer = "carrot"  
    elif obj == 3:  
        infile1 = open("flyyes.txt", "r")  
        infile2 = open("flycanbe.txt", "r")  
        answer = "fly"  
    elif obj == 4:  
        infile1 = open("caryes.txt", "r")  
        infile2 = open("carcanbe.txt", "r")  
        answer = "car"  
    print(answer)  
    return infile1, infile2, answer  

def startAsking(infile1, infile2):  
    count = 1  
    tfile = tempfile.TemporaryFile()  
    while count <= 20:  
        ask = input("Is it/Does it have: ")  
        if ask.isalpha():  
            if ask.lower() in tfile:  
                print("You've already asked this.\n")  
            else:  
                with gzip.open(tfile+".gz","wb") as f_out:  
                    f_out.write(bytes(ask, 'UTF-8'))  
                if ask.lower() in infile1.split():  
                    print("Yes it is/Yes it could\n")  
                    count = count + 1  
                elif ask.lower() in infile2.split():  
                    print("It can be/It could\n")  
                    count = count + 1  
                else:  
                    print("No or not sure\n")  
                    count = count + 1  
        else:  
            print("No numbers or symbols please.\n")  

    infile1.close()  
    infile2.close()  
    tfile.close()  

def guessingTime(answer):  
    print("That's 20! Time to guess.\n")  
    guess = eval(input("Is it a(n): "))  
    if guess.lower() == answer:  
        print("You got it! Congratulations!\n")  
    else:  
        print("Sorry, but the answer was\n")  

def main():  
    infile1, infile2, answer = getAnswer()  
    startAsking(infile1, infile2)  
    guessingTime(answer)  

main()  `#Brian Reser   H787A975
#Python Project
#Program plays 20 questions with the user. It randomly pulls a text file for the answer and keeps track of the user's answers.

import random
import turtle
import tempfile
import gzip

def getAnswer():
    obj = random.randrange(5)
    if obj == 0:
        infile1 = open("appleyes.txt", "r")
        infile2 = open("applecanbe.txt", "r")
        answer = "apple"
    elif obj == 1:
        infile1 = open("dogyes.txt", "r")
        infile2 = open("dogcanbe.txt", "r")
        answer = "dog"
    elif obj == 2:
        infile1 = open("carrotyes.txt", "r")
        infile2 = open("carrotcanbe.txt", "r")
        answer = "carrot"
    elif obj == 3:
        infile1 = open("flyyes.txt", "r")
        infile2 = open("flycanbe.txt", "r")
        answer = "fly"
    elif obj == 4:
        infile1 = open("caryes.txt", "r")
        infile2 = open("carcanbe.txt", "r")
        answer = "car"
    print(answer)
    return infile1, infile2, answer

def startAsking(infile1, infile2):
    count = 1
    tfile = tempfile.TemporaryFile()
    while count <= 20:
        ask = input("Is it/Does it have: ")
        if ask.isalpha():
            if ask.lower() in tfile:
                print("You've already asked this.\n")
            else:
                with gzip.open(tfile+".gz","wb") as f_out:
                    f_out.write(bytes(ask, 'UTF-8'))
                if ask.lower() in infile1.split():
                    print("Yes it is/Yes it could\n")
                    count = count + 1
                elif ask.lower() in infile2.split():
                    print("It can be/It could\n")
                    count = count + 1
                else:
                    print("No or not sure\n")
                    count = count + 1
        else:
            print("No numbers or symbols please.\n")

    infile1.close()
    infile2.close()
    tfile.close()

def guessingTime(answer):
    print("That's 20! Time to guess.\n")
    guess = eval(input("Is it a(n): "))
    if guess.lower() == answer:
        print("You got it! Congratulations!\n")
    else:
        print("Sorry, but the answer was\n")

def main():
    infile1, infile2, answer = getAnswer()
    startAsking(infile1, infile2)
    guessingTime(answer)

main()

当它到达将字符串“ask”写入tempfile的部分时出现错误。我该如何解决?

  

TypeError:+:'_TemporaryFileWrapper'和'str'

不支持的操作数类型

1 个答案:

答案 0 :(得分:0)

问题是tfile是文件对象而不是字符串。通过这样做来修复它:

filename = tfile.name
fullfilename = filename + ".gz"
gzip.open(fullfilename,"wb")

有关详细信息,请参阅http://docs.python.org/2/library/tempfile.html