【问题标题】:Why is my bot not replacing the file if it already exists?如果文件已经存在,为什么我的机器人不替换文件?
【发布时间】:2021-08-31 20:00:25
【问题描述】:

我正在尝试关注tutorial,了解如何为不和谐构建音乐机器人。

我在 win10 上使用 pycharm 2021.1.1 和 anaconda 3 作为我的解释器(python 3.7)。

我添加了一些更改,但没有任何更改会导致此行为:机器人尝试下载但由于文件已存在而失败。该文件很可能是song.mp3,但它应该在该行中被删除:(我尝试了两个版本,都没有工作,我认为这是同一个问题)

  1. 为什么文件song.mp3没有被删除?
  2. 我是否犯过任何其他我不知道的错误并且会阻止机器人播放音乐?
if song_there:
#        os.remove('song.mp3')
        os.remove(os.path.join(os.getcwd(),"song.mp3"))

最少的代码:

import discord
from discord.ext import commands
import youtube_dl #for url music command
import os

client = commands.Bot(command_prefix = '><')

#play music from url
@client.command(aliases = ['p'])
async def playMusic(ctx, vcName, url : str): #play music file
    song_there = os.path.isfile('song.mp3')

    try:
        if song_there:
#            os.remove('song.mp3')
            os.remove(os.path.join(os.getcwd(),"song.mp3"))
    except PermissionError:
        await ctx.send('A song is currently playing')
        return

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }]
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir('./'):
        os.rename(file, 'song.mp3')
    voice.play(discord.FFmpegPCAudio('song.mp3'))
    voiceChannel = discord.utils.get(ctx.guild.channels, name=str(vcName)) 
    await voiceChannel.connect()
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    client.run('token')

错误:

[youtube] bvNZeh6f8vE: Downloading webpage

[download] Destination: title-bvNZeh6f8vE.webm

[download] 100% of 3.95MiB in 00:00                  

[ffmpeg] Destination: title-bvNZeh6f8vE.mp3

Ignoring exception in command playMusic:

Traceback (most recent call last):

File "C:\ProgramData\Anaconda3\lib\site-packages\discord\ext\commands\core.py", line 85, in 
wrapped

ret = await coro(*args, **kwargs)

File "path/tut-bot.py", line 42, in playMusic
os.rename(file, 'song.mp3')

FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'music bot 
1.py' -> 'song.mp3'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

 File "C:\ProgramData\Anaconda3\lib\site-packages\discord\ext\commands\bot.py", line 939, in 
invoke

await ctx.command.invoke(ctx)

File "C:\ProgramData\Anaconda3\lib\site-packages\discord\ext\commands\core.py", line 863, in 
invoke

await injected(*ctx.args, **ctx.kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\discord\ext\commands\core.py", line 94, in 
wrapped

raise CommandInvokeError(exc) from exc

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: FileExistsError: 
[WinError 183] Cannot create a file when that file already exists: 'music bot 1.py' -> 
'song.mp3'

Deleting original file title-bvNZeh6f8vE.webm (pass -k to keep)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我认为它没有被删除的原因是因为你的song_there = os.path.isfile('song.mp3')尝试用song_there = os.path.exists('song.mp3')替换它,如果这个指向song.mp3的路径是正确的。

    尝试使用os.path.exists() 代替os.path.isfile(),isfile() 仅返回True 给定路径是文件。

    【讨论】:

    • 我更新了我的答案。你能用python控制台查看os.path.exists('song.mp3')是否为真吗,你需要先输入import os
    • 这是朝着正确方向迈出的一步,但它现在给了我except PermissionError,所以它不存在。一方面我不明白它怎么可能不存在但因为它已经存在而无法下载......
    • 对于except PermissionError,我认为这是因为song.mp3所在的文件夹。您只有只读权限。所以, os.remove() 导致了这个错误。尝试只使用文件资源管理器并转到此路径并右键单击文件夹到其属性并取消选中只读。对于连接,您可能需要检查您是否正确设置了频道 ID 等所有内容。
    • 尝试使用文件资源管理器并转到此路径并右键单击文件夹到其属性并取消选中只读。
    • 我认为这个网址:可以帮助:stackoverflow.com/questions/1213706/…。另外,您可以尝试使用 os.rename(old name, new name)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2021-12-07
    • 2013-11-07
    相关资源
    最近更新 更多