Python-open()是';行不通
所以基本上我试图创建一个包含黑名单模板内容的文件。我认为当您使用“w”模式时,它会为您创建一个新文件。我做错了什么?回溯与您的代码不匹配。我无意中删除了~对不起,您关于文件创建的看法是对的,但它不创建目录。此错误意味着您的根目录Python-open()是';行不通,python,discord,Python,Discord,所以基本上我试图创建一个包含黑名单模板内容的文件。我认为当您使用“w”模式时,它会为您创建一个新文件。我做错了什么?回溯与您的代码不匹配。我无意中删除了~对不起,您关于文件创建的看法是对的,但它不创建目录。此错误意味着您的根目录/(或者如果错误显示为~/Blacklisted…)中没有黑名单/目录。Python正在查看文件系统根目录中的/Blacklisted/,而不是主目录中的目录。正如Ignacio所说,您的回溯与代码不匹配。仔细查看错误消息:blacklist=open(os.path.e
/
(或者如果错误显示为~/Blacklisted…
)中没有黑名单/
目录。Python正在查看文件系统根目录中的/Blacklisted/
,而不是主目录中的目录。正如Ignacio所说,您的回溯与代码不匹配。仔细查看错误消息:blacklist=open(os.path.expanduser(“/blacklist/{}.json”)…
没有~
可供expanduser()
展开。
#BLACKLIST PROFILE CREATION COMMAND
@bot.command(pass_context=True)
async def blacklist(ctx, removeoradd, user: discord.Member, *, reason):
if ctx.message.author.id == developerID:
if removeoradd == "add":
case_id = "BL-" + id_generator(10, "1234567890")
blacklist_template = "{\"" + user.id + "\": {\"userid\": \"" + user.id + "\", \"reason\": \"" + reason + "\", \"banned_by\": \"" + ctx.message.author.id + "\", \"case_id\": \"" + case_id + "\", \"active\": \"True\"}}"
# with open("C:/Blacklisted/{}.json".format(ctx.message.author.id), "w") as json_file:
# json.dump(blacklist_template, json_file)
blacklist = open(os.path.expanduser("~/Blacklisted/{}.json".format(ctx.message.author.id)), "x")
blacklist.write("{}".format(blacklist_template))
blacklist.close()
embed = discord.Embed(title="Admin Control", description="Blacklisted:\n``{}/{}``".format(user.name, user.id))
embed.add_field(name="Reason", value=reason)
embed.add_field(name="Case ID", value=case_id)
embed.set_footer(text="v" + version + " | " + localtime)
await bot.say(embed=embed)
embed = discord.Embed(title="You can no longer create a profile.", description="If you think this is a mistake do {}appeal <reason>".format(prefix))
embed.add_field(name="Reason", value=reason)
embed.add_field(name="Case ID", value=case_id)
embed.set_footer(text="v" + version + " | " + localtime)
await bot.send_message(ctx.message.author, embed=embed)
elif removeoradd == "remove":
pass
else:
embed=discord.Embed(title="An error occured", description="Invalid argument.\nInsert ``add`` or ``remove``.", color=0xc20000)
await bot.say(embed=embed)
else:
embed=discord.Embed(title="An error occured", description="No permission.\nYou need to be a verified developer to do this.", color=0xc20000)
await bot.say(embed=embed)
File "run.py", line 126, in blacklist
blacklist = open(os.path.expanduser("/Blacklisted/{}.json".format(ctx.message.author.id)), "x")
FileNotFoundError: [Errno 2] No such file or directory: '/Blacklisted/182288858782629888.json'