我的Python脚本遇到了一个问题.代码会执行,但在它显示缓存错误之后:

Couldn't read cache at: .cache
Couldn't write token to cache at: .cache
Couldn't read cache at: .cache
Couldn't write token to cache at: .cache
Couldn't read cache at: .cache
Couldn't write token to cache at: .cache
Couldn't read cache at: .cache
Couldn't write token to cache at: .cache
Couldn't read cache at: .cache
Couldn't write token to cache at: .cache

我正在使用Spotify返回播放列表中的歌曲.然后,我会判断播放列表中的脏话,过滤掉不能播放给子元素听的歌曲.

以下是我使用的代码:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

spotify_id = "ABC123"
spotify_secret = "456789"

oldies = "738hgt"
kids = "201hgj"
country = "099sdt"

spotify_playlist = country

import lyricsgenius

genius_token = "123456789&ABCEFGHIJKLMNOPQRSTUVWXYZ"
genius = lyricsgenius.Genius(genius_token)
genius.verbose = False
genius.timeout = 100

word_listing = ["bad", "words", "go", "here"]

credentials = SpotifyClientCredentials(client_id=spotify_id, client_secret=spotify_secret)
validate = spotipy.Spotify(auth_manager=credentials)

songs = []

limit = 100

offset = 0

playlist = validate.playlist_tracks(spotify_playlist, limit=limit, offset=offset)

while True:
    playlist = validate.playlist_tracks(spotify_playlist, limit=limit, offset=offset)

    if not len(playlist['items']):
        break
    for items in playlist['items']:
        info = {'artist': items['track']['artists'][0]['name'], 'title': items['track']['name']}
        songs.append(info)

    offset += limit

print("Checking playlist...")

for song in songs:
    print(f"    Checking \"{song['title']}\" by: {song['artist']}")
    term = genius.search_song(title=song['title'], artist=song['artist'])

    for words in word_listing:
        try:
            if len(term.lyrics) > 10000:
                break
            if words in term.lyrics.lower():
                print(f"      *Found \"{words}\" in \"{song['title']}\" by: {song['artist']}")
                continue
        except AttributeError:
            print(f"        *Unable to find lyrics for: \"{song['title']}\" by: {song['artist']}")
            break
        except:
            print(f"    *Unable to connect to service, moving on...")

我已经替换了本例的所有变量值.有人告诉我,这是Spotify API的一个问题;这只是一个可以忽略的警告.

我还被告知这是一个权限问题,Spotify想要写入缓存目录,但它没有这样做的正确权限.

我真的不确定是什么导致了这个问题.错误出现在脚本的开头,然后在它显示之后,脚本的其余部分成功运行.

是不是我的某个论点或陈述中有什么东西导致了这种情况?

推荐答案

我认为这是一个权限问题,要解决您可以创建一个缓存目录然后设置适当的权限的问题,下面是您可以这样做的方法:

import os

cache_dir = '.cache'
if not os.path.exists(cache_dir):
    os.makedirs(cache_dir)

os.chmod(cache_dir, 0o700)

只需在创建SpotifyClientCredentials对象之前将此脚本放在代码的开头即可.

Python相关问答推荐

Mistral模型为不同的输入文本生成相同的嵌入

如何在类和classy-fastapi -fastapi- followup中使用FastAPI创建路由

如何使用根据其他值相似的列从列表中获取的中间值填充空NaN数据

Julia CSV for Python中的等效性Pandas index_col参数

无法使用requests或Selenium抓取一个href链接

梯度下降:简化要素集的运行时间比原始要素集长

计算每个IP的平均值

改进大型数据集的框架性能

Django—cte给出:QuerySet对象没有属性with_cte''''

如何使用SentenceTransformers创建矢量嵌入?

将scipy. sparse矩阵直接保存为常规txt文件

如何在PySide/Qt QColumbnView中删除列

用SymPy在Python中求解指数函数

ModuleNotFoundError:没有模块名为x时try 运行我的代码''

Polars Group by描述扩展

无法在Spyder上的Pandas中将本地CSV转换为数据帧

比较两个有条件的数据帧并删除所有不合格的数据帧

普洛特利express 发布的人口普查数据失败

Scipy.linprog的可行性有问题吗?(A_ub@x0<;=b_ub).all()为True-但是-linprog(np.zeros_like(X0),A_ub=A_ub,b_ub=b_ub)不可行

GEKKO中若干参数的线性插值动态优化