TypeError:期望一个字符缓冲区对象(尝试写入文本文件)

时间:2014-11-09 17:26:40

标签: python-2.7 typeerror

我正在尝试编写一个简单的程序,将函数的输出写入文本文件,我让它工作,但今天我尝试运行它,它在第54行给出了我的错误:TypeError:expect a a字符缓冲对象。

以下是该行:

f.write(spotify.getCurrentTrack())

此外,这是代码的其余部分:

from pytify import Spotify
spotify = Spotify()
import time

var2exit = 1

print "This program was created by Drew Halverson. Do not claim as your own work."
time.sleep(2)
print "This program could not have been created without the help of pytify."
time.sleep(2)
print "Also, this program is not affiliated with Spotify at all and is not an official app."
time.sleep(2)
print "For information on how to use this program and what its use is, check readme.txt"

yes = set(["yes", "y", "YES"])
no = set(["no", "n", "NO"])

r = open("readme.txt", "r")

print "Would you like to read the readme now (type yes or no)?"

choice = raw_input()

if choice in yes: print r.read()
print " "
time.sleep(2)
if choice in no: print "Hi! I will now be checking what song is playing on spotify."
time.sleep(2)
print "Make sure spotify is playing (Only the downloaded application will work. The program is not compatible with the web player version of Spotify."
time.sleep(2)
print " "
time.sleep(1)
print "Have you downloaded Spotify on this computer already? Type yes or no..."

choice2 = raw_input()

if choice2 in yes: print "Alright! Let's go!"
if choice2 in no: var2exit = 2

if var2exit == 2:
    print "Go install Spotify first, then try again."
    time.sleep(5)
    print "Goodbye!"
    time.sleep(2)
    sys.exit()

var = 1

while var == 1 :
    spotify.getCurrentTrack()
    spotify.getCurrentArtist()
    f = open("current_track.txt", "w")
    g = open("current_artist.txt", "w")
    f.write(spotify.getCurrentTrack())
    g.write(spotify.getCurrentArtist())
    print "The current Track is:"
    print spotify.getCurrentTrack()
    print "The current Artist is:"
    print spotify.getCurrentArtist()
    time.sleep(10)
    print" "
    print "Checking again..."

我知道有类似的问题,但我找不到任何问题。感谢。

0 个答案:

没有答案