Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 TypeError:应为字符缓冲区对象(尝试写入文本文件)_Python 2.7_Typeerror - Fatal编程技术网

Python 2.7 TypeError:应为字符缓冲区对象(尝试写入文本文件)

Python 2.7 TypeError:应为字符缓冲区对象(尝试写入文本文件),python-2.7,typeerror,Python 2.7,Typeerror,我试图编写一个简单的程序,将函数的输出写入一个文本文件,我让它工作了,但今天我尝试运行它,它在第54行给出了一个错误:TypeError:需要一个字符缓冲区对象 这是一行: f.write(spotify.getCurrentTrack()) 此外,以下是代码的其余部分: from pytify import Spotify spotify = Spotify() import time var2exit = 1 print "This program was created by Dre

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

这是一行:

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..."

我知道有类似的问题,但我发现没有一个能完全回答我的问题。谢谢。

查看getCurrentTrack的文档-它返回的是什么类型?很抱歉,我不明白您所说的文档是什么意思。文档:说明、说明、帮助文件,以及您拥有的任何内容。在没有文档的情况下,回答您的问题需要库源代码或至少是标题(如果有)。