绝对路径在硬编码时有效,但在python中存储在变量中时无效



我正在尝试编写一个python bot,它只需将目录中的大量文件上传到服务器即可。(大多数是带有一些屏幕截图的游戏片段。(问题是,当我动态传递文件路径时,我会得到一个找不到文件的错误。通过硬编码时效果良好。我已经打印,甚至发送了不和谐的文件路径,它是正确的。尝试了.strip((和.encode('unicode-escape'(以及其他各种选项,但没有找到任何有效的选项。这让我有点困惑。有什么想法吗?

import os
import discord
import time
from discord.ext import commands
client = commands.Bot(command_prefix = '!!')

#locations to upload
locations = [
'/root/discord/',
'/home/discord',

]
#file types to not upload
bad_files = [
'viminfo',
'txt',
'sh',
'',
'bat',

]
#walk through directory and upload files
async def dir_walk(ctx,p):
for roots,dirs,files in os.walk(p):
for i in dirs:
for x in files:
#check to see if file extension matches one listed to not upload.
if x.split('.')[-1] in bad_files:
pass
else:
try:
#upload files
file_path = os.path.join(roots,i,x)
f = open(full_path,'rb')
await ctx.send(i,file = discord.File(f,filename = x))
time.sleep(5)
except:
raise
time.sleep(5)
@client.command(pass_context=True, name="walk")
async def list_dir(ctx):
for x in locations:
await dir_walk(ctx, x)

client.run('')

回溯是:

Ignoring exception in command walk:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/discord/ext/commands/core.py", li                                                                                        ne 85, in wrapped
ret = await coro(*args, **kwargs)
File "newwalk.py", line 50, in list_dir
await dir_walk(ctx,x)
File "newwalk.py", line 40, in dir_walk
f = open(x,'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'ss dec_2019_1_20_0008.jpg'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/discord/ext/commands/bot.py", lin                                                                                        e 939, in invoke
await ctx.command.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/discord/ext/commands/core.py", li                                                                                        ne 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/usr/local/lib/python3.8/dist-packages/discord/ext/commands/core.py", li                                                                                        ne 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Fil                                                                                        eNotFoundError: [Errno 2] No such file or directory: 'ss dec_2019_1_20_0008.jpg'

我设法找到了一种方法。这将需要更多的工作,但我更改了一些代码。给你。

import os
import discord
import time
from discord.ext import commands
client = commands.Bot(command_prefix = '!!')

#locations to upload
locations = [
'',

]
#file types to not upload
good_files = [
'png',
'jpg',
'jpeg',
'mp4',
'mpg',
'mpeg',
'wav',
'flv',
'mov',
'gif',
'tif',
'bmp',


]
#walk through directory and upload files
async def dir_walk(ctx,p):
for roots,dirs,files in os.walk(p):
for i in dirs:
os.chdir(os.path.join(roots,i))
for x in os.listdir('.'):
if os.path.isfile(x):
if x.split('.')[-1] in good_files:
try:
with open(x,'rb') as f:
await ctx.send(i,file = discord.File(f,filename = x))
time.sleep(1)
except:
pass


@client.command(pass_context=True, name="walk")
async def list_dir(ctx):
for x in locations:
await dir_walk(ctx,x)
client.run('')

相关内容

  • 没有找到相关文章