Reputation: 448
Trying out discord.py. Have set up a simple error that's supposed to appear in the log file. Here's the code:
@client.event
async def on_message(message):
if message.content == 'raise-exception':
raise discord.DiscordException
@client.event
async def on_error(event, *args, **kwargs):
with open('err.log', 'a+') as f:
if event == 'on_message':
log_entry = 'log entry'
f.write(log_entry)
else:
raise
When an error is being triggered err.log
file doesn't get updated. Nothing happens whatsoever.
If I add print(log_entry)
, it prints out log entry in console (which only means that condition was met) but still doesn't do anything to the err.log
file. I've also tried using .txt file instead, and it doesn't work either.
Upvotes: 0
Views: 359
Reputation: 43840
Just want to recommend pathlib
it can make path handling easier.
With pathlib you can create the file relative to the source like this:
from pathlib import Path
SOURCE_DIRECTORY = Path(__file__).parent
@client.event
async def on_error(event, *args, **kwargs):
errorlog_filepath = SOURCE_DIRECTORY / 'err.log'
with errorlog_filepath.open('a+') as f:
if event == 'on_message':
log_entry = 'log entry'
f.write(log_entry)
else:
raise
Upvotes: 1
Reputation: 2092
When you do not specify any path while reading or writing a file, it takes the current working directory (cwd)
as your base path.
In your case when you open
the file with a+
mode, it must be creating a file at your cwd
.
CWD is the path from where you invoked your python script. You can check it from python by :
import os
print(os.getcwd())
So if you do not provide any path and just write the file name, this is the path where your files will be written. If you want to use relative path you can do so after looking at your cwd
and creating relative path string accordingly.
Upvotes: 1